1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
3 // This file defines the main TableGen data structures, including the TableGen
4 // types, values, and high-level data structures.
6 //===----------------------------------------------------------------------===//
17 // RecTy subclasses...
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
52 virtual void print(std::ostream &OS) const = 0;
55 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
56 /// converted to the specified type.
57 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
59 public: // These methods should only be called from subclasses of Init
60 virtual Init *convertValue( UnsetInit *UI) { return 0; }
61 virtual Init *convertValue( BitInit *BI) { return 0; }
62 virtual Init *convertValue( BitsInit *BI) { return 0; }
63 virtual Init *convertValue( IntInit *II) { return 0; }
64 virtual Init *convertValue(StringInit *SI) { return 0; }
65 virtual Init *convertValue( ListInit *LI) { return 0; }
66 virtual Init *convertValue( CodeInit *CI) { return 0; }
67 virtual Init *convertValue(VarBitInit *VB) { return 0; }
68 virtual Init *convertValue( DefInit *DI) { return 0; }
69 virtual Init *convertValue( TypedInit *TI) { return 0; }
70 virtual Init *convertValue( VarInit *VI) {
71 return convertValue((TypedInit*)VI);
73 virtual Init *convertValue( FieldInit *FI) {
74 return convertValue((TypedInit*)FI);
77 public: // These methods should only be called by subclasses of RecTy.
78 // baseClassOf - These virtual methods should be overloaded to return true iff
79 // all values of type 'RHS' can be converted to the 'this' type.
80 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
81 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
82 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
83 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
84 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
85 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
86 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
87 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
90 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
96 /// BitRecTy - 'bit' - Represent a single bit
98 struct BitRecTy : public RecTy {
99 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
100 Init *convertValue(BitInit *BI) { return (Init*)BI; }
101 Init *convertValue(BitsInit *BI);
102 Init *convertValue(IntInit *II);
103 Init *convertValue(TypedInit *VI);
104 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
106 void print(std::ostream &OS) const { OS << "bit"; }
108 bool typeIsConvertibleTo(const RecTy *RHS) const {
109 return RHS->baseClassOf(this);
111 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
112 virtual bool baseClassOf(const BitsRecTy *RHS) const;
113 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
117 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
119 class BitsRecTy : public RecTy {
122 BitsRecTy(unsigned Sz) : Size(Sz) {}
124 unsigned getNumBits() const { return Size; }
126 Init *convertValue(UnsetInit *UI);
127 Init *convertValue(BitInit *UI);
128 Init *convertValue(BitsInit *BI);
129 Init *convertValue(IntInit *II);
130 Init *convertValue(TypedInit *VI);
132 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
134 bool typeIsConvertibleTo(const RecTy *RHS) const {
135 return RHS->baseClassOf(this);
137 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
138 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
139 virtual bool baseClassOf(const BitsRecTy *RHS) const {
140 return RHS->Size == Size;
145 /// IntRecTy - 'int' - Represent an integer value of no particular size
147 struct IntRecTy : public RecTy {
148 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
149 Init *convertValue(IntInit *II) { return (Init*)II; }
150 Init *convertValue(BitInit *BI);
151 Init *convertValue(BitsInit *BI);
152 Init *convertValue(TypedInit *TI);
154 void print(std::ostream &OS) const { OS << "int"; }
156 bool typeIsConvertibleTo(const RecTy *RHS) const {
157 return RHS->baseClassOf(this);
160 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
161 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
162 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
165 /// StringRecTy - 'string' - Represent an string value
167 struct StringRecTy : public RecTy {
168 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
169 Init *convertValue(StringInit *SI) { return (Init*)SI; }
170 Init *convertValue(TypedInit *TI);
171 void print(std::ostream &OS) const { OS << "string"; }
173 bool typeIsConvertibleTo(const RecTy *RHS) const {
174 return RHS->baseClassOf(this);
177 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
180 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
181 /// the specified type.
183 class ListRecTy : public RecTy {
186 ListRecTy(RecTy *T) : Ty(T) {}
188 RecTy *getElementType() const { return Ty; }
190 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
191 Init *convertValue(ListInit *LI);
192 Init *convertValue(TypedInit *TI);
194 void print(std::ostream &OS) const;
196 bool typeIsConvertibleTo(const RecTy *RHS) const {
197 return RHS->baseClassOf(this);
200 virtual bool baseClassOf(const ListRecTy *RHS) const {
201 return RHS->getElementType()->typeIsConvertibleTo(Ty);
205 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
207 struct CodeRecTy : public RecTy {
208 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
209 Init *convertValue( CodeInit *CI) { return (Init*)CI; }
211 void print(std::ostream &OS) const { OS << "code"; }
213 bool typeIsConvertibleTo(const RecTy *RHS) const {
214 return RHS->baseClassOf(this);
216 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
219 /// DagRecTy - 'dag' - Represent a dag fragment
221 struct DagRecTy : public RecTy {
222 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
223 //Init *convertValue( DagInit *CI) { return (Init*)CI; }
224 Init *convertValue(TypedInit *TI);
226 void print(std::ostream &OS) const { OS << "dag"; }
228 bool typeIsConvertibleTo(const RecTy *RHS) const {
229 return RHS->baseClassOf(this);
231 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
235 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
238 class RecordRecTy : public RecTy {
241 RecordRecTy(Record *R) : Rec(R) {}
243 Record *getRecord() const { return Rec; }
245 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
246 Init *convertValue( DefInit *DI);
247 Init *convertValue(TypedInit *VI);
249 void print(std::ostream &OS) const;
251 bool typeIsConvertibleTo(const RecTy *RHS) const {
252 return RHS->baseClassOf(this);
254 virtual bool baseClassOf(const RecordRecTy *RHS) const;
259 //===----------------------------------------------------------------------===//
260 // Initializer Classes
261 //===----------------------------------------------------------------------===//
266 /// isComplete - This virtual method should be overridden by values that may
267 /// not be completely specified yet.
268 virtual bool isComplete() const { return true; }
270 /// print - Print out this value.
271 virtual void print(std::ostream &OS) const = 0;
273 /// dump - Debugging method that may be called through a debugger, just
274 /// invokes print on cerr.
277 /// convertInitializerTo - This virtual function is a simple call-back
278 /// function that should be overridden to call the appropriate
279 /// RecTy::convertValue method.
281 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
283 /// convertInitializerBitRange - This method is used to implement the bitrange
284 /// selection operator. Given an initializer, it selects the specified bits
285 /// out, returning them as a new init of bits type. If it is not legal to use
286 /// the bit subscript operator on this initializer, return null.
288 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
292 /// getFieldType - This method is used to implement the FieldInit class.
293 /// Implementors of this method should return the type of the named field if
294 /// they are of record type.
296 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
298 /// getFieldInit - This method complements getFieldType to return the
299 /// initializer for the specified field. If getFieldType returns non-null
300 /// this method should return non-null, otherwise it returns null.
302 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
306 /// resolveReferences - This method is used by classes that refer to other
307 /// variables which may not be defined at the time they expression is formed.
308 /// If a value is set for the variable later, this method will be called on
309 /// users of the value to allow the value to propagate out.
311 virtual Init *resolveReferences(Record &R) { return this; }
314 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
315 I.print(OS); return OS;
319 /// UnsetInit - ? - Represents an uninitialized value
321 struct UnsetInit : public Init {
322 virtual Init *convertInitializerTo(RecTy *Ty) {
323 return Ty->convertValue(this);
326 virtual bool isComplete() const { return false; }
327 virtual void print(std::ostream &OS) const { OS << "?"; }
331 /// BitInit - true/false - Represent a concrete initializer for a bit.
333 class BitInit : public Init {
336 BitInit(bool V) : Value(V) {}
338 bool getValue() const { return Value; }
340 virtual Init *convertInitializerTo(RecTy *Ty) {
341 return Ty->convertValue(this);
344 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
347 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
348 /// It contains a vector of bits, whose size is determined by the type.
350 class BitsInit : public Init {
351 std::vector<Init*> Bits;
353 BitsInit(unsigned Size) : Bits(Size) {}
355 unsigned getNumBits() const { return Bits.size(); }
357 Init *getBit(unsigned Bit) const {
358 assert(Bit < Bits.size() && "Bit index out of range!");
361 void setBit(unsigned Bit, Init *V) {
362 assert(Bit < Bits.size() && "Bit index out of range!");
363 assert(Bits[Bit] == 0 && "Bit already set!");
367 virtual Init *convertInitializerTo(RecTy *Ty) {
368 return Ty->convertValue(this);
370 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
372 virtual bool isComplete() const {
373 for (unsigned i = 0; i != getNumBits(); ++i)
374 if (!getBit(i)->isComplete()) return false;
377 virtual void print(std::ostream &OS) const;
379 virtual Init *resolveReferences(Record &R);
381 // printXX - Print this bitstream with the specified format, returning true if
382 // it is not possible.
383 bool printInHex(std::ostream &OS) const;
384 bool printAsVariable(std::ostream &OS) const;
385 bool printAsUnset(std::ostream &OS) const;
389 /// IntInit - 7 - Represent an initalization by a literal integer value.
391 class IntInit : public Init {
394 IntInit(int V) : Value(V) {}
396 int getValue() const { return Value; }
398 virtual Init *convertInitializerTo(RecTy *Ty) {
399 return Ty->convertValue(this);
401 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
403 virtual void print(std::ostream &OS) const { OS << Value; }
407 /// StringInit - "foo" - Represent an initialization by a string value.
409 class StringInit : public Init {
412 StringInit(const std::string &V) : Value(V) {}
414 const std::string &getValue() const { return Value; }
416 virtual Init *convertInitializerTo(RecTy *Ty) {
417 return Ty->convertValue(this);
420 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
423 /// CodeInit - "[{...}]" - Represent a code fragment.
425 class CodeInit : public Init {
428 CodeInit(const std::string &V) : Value(V) {}
430 const std::string getValue() const { return Value; }
432 virtual Init *convertInitializerTo(RecTy *Ty) {
433 return Ty->convertValue(this);
436 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
439 /// ListInit - [AL, AH, CL] - Represent a list of defs
441 class ListInit : public Init {
442 std::vector<Init*> Values;
444 ListInit(std::vector<Init*> &Vs) {
448 unsigned getSize() const { return Values.size(); }
449 Init *getElement(unsigned i) const {
450 assert(i < Values.size() && "List element index out of range!");
454 virtual Init *convertInitializerTo(RecTy *Ty) {
455 return Ty->convertValue(this);
458 virtual void print(std::ostream &OS) const;
462 /// TypedInit - This is the common super-class of types that have a specific,
465 class TypedInit : public Init {
468 TypedInit(RecTy *T) : Ty(T) {}
470 RecTy *getType() const { return Ty; }
472 /// resolveBitReference - This method is used to implement
473 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
474 /// simply return the resolved value, otherwise we return this.
476 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
479 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
481 class VarInit : public TypedInit {
484 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
486 virtual Init *convertInitializerTo(RecTy *Ty) {
487 return Ty->convertValue(this);
490 const std::string &getName() const { return VarName; }
492 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
494 virtual Init *resolveBitReference(Record &R, unsigned Bit);
496 virtual RecTy *getFieldType(const std::string &FieldName) const;
497 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
499 /// resolveReferences - This method is used by classes that refer to other
500 /// variables which may not be defined at the time they expression is formed.
501 /// If a value is set for the variable later, this method will be called on
502 /// users of the value to allow the value to propagate out.
504 virtual Init *resolveReferences(Record &R);
506 virtual void print(std::ostream &OS) const { OS << VarName; }
510 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
512 class VarBitInit : public Init {
516 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
517 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
518 ((BitsRecTy*)T->getType())->getNumBits() > B &&
519 "Illegal VarBitInit expression!");
522 virtual Init *convertInitializerTo(RecTy *Ty) {
523 return Ty->convertValue(this);
526 TypedInit *getVariable() const { return TI; }
527 unsigned getBitNum() const { return Bit; }
529 virtual void print(std::ostream &OS) const {
530 TI->print(OS); OS << "{" << Bit << "}";
532 virtual Init *resolveReferences(Record &R);
536 /// DefInit - AL - Represent a reference to a 'def' in the description
538 class DefInit : public Init {
541 DefInit(Record *D) : Def(D) {}
543 virtual Init *convertInitializerTo(RecTy *Ty) {
544 return Ty->convertValue(this);
547 Record *getDef() const { return Def; }
549 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
551 virtual RecTy *getFieldType(const std::string &FieldName) const;
552 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
554 virtual void print(std::ostream &OS) const;
558 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
560 class FieldInit : public TypedInit {
561 Init *Rec; // Record we are referring to
562 std::string FieldName; // Field we are accessing
564 FieldInit(Init *R, const std::string &FN)
565 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
566 assert(getType() && "FieldInit with non-record type!");
569 virtual Init *convertInitializerTo(RecTy *Ty) {
570 return Ty->convertValue(this);
573 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
575 virtual Init *resolveBitReference(Record &R, unsigned Bit);
577 virtual Init *resolveReferences(Record &R);
579 virtual void print(std::ostream &OS) const {
580 Rec->print(OS); OS << "." << FieldName;
585 //===----------------------------------------------------------------------===//
586 // High-Level Classes
587 //===----------------------------------------------------------------------===//
595 RecordVal(const std::string &N, RecTy *T, unsigned P);
597 const std::string &getName() const { return Name; }
599 unsigned getPrefix() const { return Prefix; }
600 RecTy *getType() const { return Ty; }
601 Init *getValue() const { return Value; }
603 bool setValue(Init *V) {
605 Value = V->convertInitializerTo(Ty);
613 void print(std::ostream &OS, bool PrintSem = true) const;
616 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
622 const std::string Name;
623 std::vector<std::string> TemplateArgs;
624 std::vector<RecordVal> Values;
625 std::vector<Record*> SuperClasses;
628 Record(const std::string &N) : Name(N) {}
631 const std::string &getName() const { return Name; }
632 const std::vector<std::string> &getTemplateArgs() const {
635 const std::vector<RecordVal> &getValues() const { return Values; }
636 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
638 bool isTemplateArg(const std::string &Name) const {
639 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
640 if (TemplateArgs[i] == Name) return true;
644 const RecordVal *getValue(const std::string &Name) const {
645 for (unsigned i = 0, e = Values.size(); i != e; ++i)
646 if (Values[i].getName() == Name) return &Values[i];
649 RecordVal *getValue(const std::string &Name) {
650 for (unsigned i = 0, e = Values.size(); i != e; ++i)
651 if (Values[i].getName() == Name) return &Values[i];
655 void addTemplateArg(const std::string &Name) {
656 assert(!isTemplateArg(Name) && "Template arg already defined!");
657 TemplateArgs.push_back(Name);
660 void addValue(const RecordVal &RV) {
661 assert(getValue(RV.getName()) == 0 && "Value already added!");
662 Values.push_back(RV);
665 void removeValue(const std::string &Name) {
666 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
667 for (unsigned i = 0, e = Values.size(); i != e; ++i)
668 if (Values[i].getName() == Name) {
669 Values.erase(Values.begin()+i);
672 assert(0 && "Name does not exist in record!");
675 bool isSubClassOf(Record *R) const {
676 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
677 if (SuperClasses[i] == R)
682 void addSuperClass(Record *R) {
683 assert(!isSubClassOf(R) && "Already subclassing record!");
684 SuperClasses.push_back(R);
687 // resolveReferences - If there are any field references that refer to fields
688 // that have been filled in, we can propagate the values now.
690 void resolveReferences();
694 //===--------------------------------------------------------------------===//
695 // High-level methods useful to tablegen back-ends
698 /// getValueInit - Return the initializer for a value with the specified name,
699 /// or throw an exception if the field does not exist.
701 Init *getValueInit(const std::string &FieldName) const;
703 /// getValueAsString - This method looks up the specified field and returns
704 /// its value as a string, throwing an exception if the field does not exist
705 /// or if the value is not a string.
707 std::string getValueAsString(const std::string &FieldName) const;
709 /// getValueAsBitsInit - This method looks up the specified field and returns
710 /// its value as a BitsInit, throwing an exception if the field does not exist
711 /// or if the value is not the right type.
713 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
715 /// getValueAsListInit - This method looks up the specified field and returns
716 /// its value as a ListInit, throwing an exception if the field does not exist
717 /// or if the value is not the right type.
719 ListInit *getValueAsListInit(const std::string &FieldName) const;
721 /// getValueAsDef - This method looks up the specified field and returns its
722 /// value as a Record, throwing an exception if the field does not exist or if
723 /// the value is not the right type.
725 Record *getValueAsDef(const std::string &FieldName) const;
727 /// getValueAsBit - This method looks up the specified field and returns its
728 /// value as a bit, throwing an exception if the field does not exist or if
729 /// the value is not the right type.
731 bool getValueAsBit(const std::string &FieldName) const;
733 /// getValueAsInt - This method looks up the specified field and returns its
734 /// value as an int, throwing an exception if the field does not exist or if
735 /// the value is not the right type.
737 int getValueAsInt(const std::string &FieldName) const;
740 std::ostream &operator<<(std::ostream &OS, const Record &R);
743 std::map<std::string, Record*> Classes, Defs;
746 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
747 E = Classes.end(); I != E; ++I)
749 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
750 E = Defs.end(); I != E; ++I)
754 const std::map<std::string, Record*> &getClasses() const { return Classes; }
755 const std::map<std::string, Record*> &getDefs() const { return Defs; }
757 Record *getClass(const std::string &Name) const {
758 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
759 return I == Classes.end() ? 0 : I->second;
761 Record *getDef(const std::string &Name) const {
762 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
763 return I == Defs.end() ? 0 : I->second;
765 void addClass(Record *R) {
766 assert(getClass(R->getName()) == 0 && "Class already exists!");
767 Classes.insert(std::make_pair(R->getName(), R));
769 void addDef(Record *R) {
770 assert(getDef(R->getName()) == 0 && "Def already exists!");
771 Defs.insert(std::make_pair(R->getName(), R));
774 //===--------------------------------------------------------------------===//
775 // High-level helper methods, useful for tablegen backends...
777 /// getAllDerivedDefinitions - This method returns all concrete definitions
778 /// that derive from the specified class name. If a class with the specified
779 /// name does not exist, an exception is thrown.
781 getAllDerivedDefinitions(const std::string &ClassName) const;
787 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
789 extern RecordKeeper Records;