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; }
210 Init *convertValue(TypedInit *TI);
212 void print(std::ostream &OS) const { OS << "code"; }
214 bool typeIsConvertibleTo(const RecTy *RHS) const {
215 return RHS->baseClassOf(this);
217 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
220 /// DagRecTy - 'dag' - Represent a dag fragment
222 struct DagRecTy : public RecTy {
223 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
224 //Init *convertValue( DagInit *CI) { return (Init*)CI; }
225 Init *convertValue(TypedInit *TI);
227 void print(std::ostream &OS) const { OS << "dag"; }
229 bool typeIsConvertibleTo(const RecTy *RHS) const {
230 return RHS->baseClassOf(this);
232 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
236 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
239 class RecordRecTy : public RecTy {
242 RecordRecTy(Record *R) : Rec(R) {}
244 Record *getRecord() const { return Rec; }
246 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
247 Init *convertValue( DefInit *DI);
248 Init *convertValue(TypedInit *VI);
250 void print(std::ostream &OS) const;
252 bool typeIsConvertibleTo(const RecTy *RHS) const {
253 return RHS->baseClassOf(this);
255 virtual bool baseClassOf(const RecordRecTy *RHS) const;
260 //===----------------------------------------------------------------------===//
261 // Initializer Classes
262 //===----------------------------------------------------------------------===//
267 /// isComplete - This virtual method should be overridden by values that may
268 /// not be completely specified yet.
269 virtual bool isComplete() const { return true; }
271 /// print - Print out this value.
272 virtual void print(std::ostream &OS) const = 0;
274 /// dump - Debugging method that may be called through a debugger, just
275 /// invokes print on cerr.
278 /// convertInitializerTo - This virtual function is a simple call-back
279 /// function that should be overridden to call the appropriate
280 /// RecTy::convertValue method.
282 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
284 /// convertInitializerBitRange - This method is used to implement the bitrange
285 /// selection operator. Given an initializer, it selects the specified bits
286 /// out, returning them as a new init of bits type. If it is not legal to use
287 /// the bit subscript operator on this initializer, return null.
289 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
293 /// getFieldType - This method is used to implement the FieldInit class.
294 /// Implementors of this method should return the type of the named field if
295 /// they are of record type.
297 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
299 /// getFieldInit - This method complements getFieldType to return the
300 /// initializer for the specified field. If getFieldType returns non-null
301 /// this method should return non-null, otherwise it returns null.
303 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
307 /// resolveReferences - This method is used by classes that refer to other
308 /// variables which may not be defined at the time they expression is formed.
309 /// If a value is set for the variable later, this method will be called on
310 /// users of the value to allow the value to propagate out.
312 virtual Init *resolveReferences(Record &R) { return this; }
315 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
316 I.print(OS); return OS;
320 /// UnsetInit - ? - Represents an uninitialized value
322 struct UnsetInit : public Init {
323 virtual Init *convertInitializerTo(RecTy *Ty) {
324 return Ty->convertValue(this);
327 virtual bool isComplete() const { return false; }
328 virtual void print(std::ostream &OS) const { OS << "?"; }
332 /// BitInit - true/false - Represent a concrete initializer for a bit.
334 class BitInit : public Init {
337 BitInit(bool V) : Value(V) {}
339 bool getValue() const { return Value; }
341 virtual Init *convertInitializerTo(RecTy *Ty) {
342 return Ty->convertValue(this);
345 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
348 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
349 /// It contains a vector of bits, whose size is determined by the type.
351 class BitsInit : public Init {
352 std::vector<Init*> Bits;
354 BitsInit(unsigned Size) : Bits(Size) {}
356 unsigned getNumBits() const { return Bits.size(); }
358 Init *getBit(unsigned Bit) const {
359 assert(Bit < Bits.size() && "Bit index out of range!");
362 void setBit(unsigned Bit, Init *V) {
363 assert(Bit < Bits.size() && "Bit index out of range!");
364 assert(Bits[Bit] == 0 && "Bit already set!");
368 virtual Init *convertInitializerTo(RecTy *Ty) {
369 return Ty->convertValue(this);
371 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
373 virtual bool isComplete() const {
374 for (unsigned i = 0; i != getNumBits(); ++i)
375 if (!getBit(i)->isComplete()) return false;
378 virtual void print(std::ostream &OS) const;
380 virtual Init *resolveReferences(Record &R);
382 // printXX - Print this bitstream with the specified format, returning true if
383 // it is not possible.
384 bool printInHex(std::ostream &OS) const;
385 bool printAsVariable(std::ostream &OS) const;
386 bool printAsUnset(std::ostream &OS) const;
390 /// IntInit - 7 - Represent an initalization by a literal integer value.
392 class IntInit : public Init {
395 IntInit(int V) : Value(V) {}
397 int getValue() const { return Value; }
399 virtual Init *convertInitializerTo(RecTy *Ty) {
400 return Ty->convertValue(this);
402 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
404 virtual void print(std::ostream &OS) const { OS << Value; }
408 /// StringInit - "foo" - Represent an initialization by a string value.
410 class StringInit : public Init {
413 StringInit(const std::string &V) : Value(V) {}
415 const std::string &getValue() const { return Value; }
417 virtual Init *convertInitializerTo(RecTy *Ty) {
418 return Ty->convertValue(this);
421 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
424 /// CodeInit - "[{...}]" - Represent a code fragment.
426 class CodeInit : public Init {
429 CodeInit(const std::string &V) : Value(V) {}
431 const std::string getValue() const { return Value; }
433 virtual Init *convertInitializerTo(RecTy *Ty) {
434 return Ty->convertValue(this);
437 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
440 /// ListInit - [AL, AH, CL] - Represent a list of defs
442 class ListInit : public Init {
443 std::vector<Init*> Values;
445 ListInit(std::vector<Init*> &Vs) {
449 unsigned getSize() const { return Values.size(); }
450 Init *getElement(unsigned i) const {
451 assert(i < Values.size() && "List element index out of range!");
455 virtual Init *convertInitializerTo(RecTy *Ty) {
456 return Ty->convertValue(this);
459 virtual void print(std::ostream &OS) const;
463 /// TypedInit - This is the common super-class of types that have a specific,
466 class TypedInit : public Init {
469 TypedInit(RecTy *T) : Ty(T) {}
471 RecTy *getType() const { return Ty; }
473 /// resolveBitReference - This method is used to implement
474 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
475 /// simply return the resolved value, otherwise we return this.
477 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
480 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
482 class VarInit : public TypedInit {
485 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
487 virtual Init *convertInitializerTo(RecTy *Ty) {
488 return Ty->convertValue(this);
491 const std::string &getName() const { return VarName; }
493 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
495 virtual Init *resolveBitReference(Record &R, unsigned Bit);
497 virtual RecTy *getFieldType(const std::string &FieldName) const;
498 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
500 /// resolveReferences - This method is used by classes that refer to other
501 /// variables which may not be defined at the time they expression is formed.
502 /// If a value is set for the variable later, this method will be called on
503 /// users of the value to allow the value to propagate out.
505 virtual Init *resolveReferences(Record &R);
507 virtual void print(std::ostream &OS) const { OS << VarName; }
511 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
513 class VarBitInit : public Init {
517 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
518 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
519 ((BitsRecTy*)T->getType())->getNumBits() > B &&
520 "Illegal VarBitInit expression!");
523 virtual Init *convertInitializerTo(RecTy *Ty) {
524 return Ty->convertValue(this);
527 TypedInit *getVariable() const { return TI; }
528 unsigned getBitNum() const { return Bit; }
530 virtual void print(std::ostream &OS) const {
531 TI->print(OS); OS << "{" << Bit << "}";
533 virtual Init *resolveReferences(Record &R);
537 /// DefInit - AL - Represent a reference to a 'def' in the description
539 class DefInit : public Init {
542 DefInit(Record *D) : Def(D) {}
544 virtual Init *convertInitializerTo(RecTy *Ty) {
545 return Ty->convertValue(this);
548 Record *getDef() const { return Def; }
550 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
552 virtual RecTy *getFieldType(const std::string &FieldName) const;
553 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
555 virtual void print(std::ostream &OS) const;
559 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
561 class FieldInit : public TypedInit {
562 Init *Rec; // Record we are referring to
563 std::string FieldName; // Field we are accessing
565 FieldInit(Init *R, const std::string &FN)
566 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
567 assert(getType() && "FieldInit with non-record type!");
570 virtual Init *convertInitializerTo(RecTy *Ty) {
571 return Ty->convertValue(this);
574 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
576 virtual Init *resolveBitReference(Record &R, unsigned Bit);
578 virtual Init *resolveReferences(Record &R);
580 virtual void print(std::ostream &OS) const {
581 Rec->print(OS); OS << "." << FieldName;
586 //===----------------------------------------------------------------------===//
587 // High-Level Classes
588 //===----------------------------------------------------------------------===//
596 RecordVal(const std::string &N, RecTy *T, unsigned P);
598 const std::string &getName() const { return Name; }
600 unsigned getPrefix() const { return Prefix; }
601 RecTy *getType() const { return Ty; }
602 Init *getValue() const { return Value; }
604 bool setValue(Init *V) {
606 Value = V->convertInitializerTo(Ty);
614 void print(std::ostream &OS, bool PrintSem = true) const;
617 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
623 const std::string Name;
624 std::vector<std::string> TemplateArgs;
625 std::vector<RecordVal> Values;
626 std::vector<Record*> SuperClasses;
629 Record(const std::string &N) : Name(N) {}
632 const std::string &getName() const { return Name; }
633 const std::vector<std::string> &getTemplateArgs() const {
636 const std::vector<RecordVal> &getValues() const { return Values; }
637 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
639 bool isTemplateArg(const std::string &Name) const {
640 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
641 if (TemplateArgs[i] == Name) return true;
645 const RecordVal *getValue(const std::string &Name) const {
646 for (unsigned i = 0, e = Values.size(); i != e; ++i)
647 if (Values[i].getName() == Name) return &Values[i];
650 RecordVal *getValue(const std::string &Name) {
651 for (unsigned i = 0, e = Values.size(); i != e; ++i)
652 if (Values[i].getName() == Name) return &Values[i];
656 void addTemplateArg(const std::string &Name) {
657 assert(!isTemplateArg(Name) && "Template arg already defined!");
658 TemplateArgs.push_back(Name);
661 void addValue(const RecordVal &RV) {
662 assert(getValue(RV.getName()) == 0 && "Value already added!");
663 Values.push_back(RV);
666 void removeValue(const std::string &Name) {
667 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
668 for (unsigned i = 0, e = Values.size(); i != e; ++i)
669 if (Values[i].getName() == Name) {
670 Values.erase(Values.begin()+i);
673 assert(0 && "Name does not exist in record!");
676 bool isSubClassOf(Record *R) const {
677 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
678 if (SuperClasses[i] == R)
683 void addSuperClass(Record *R) {
684 assert(!isSubClassOf(R) && "Already subclassing record!");
685 SuperClasses.push_back(R);
688 // resolveReferences - If there are any field references that refer to fields
689 // that have been filled in, we can propagate the values now.
691 void resolveReferences();
695 //===--------------------------------------------------------------------===//
696 // High-level methods useful to tablegen back-ends
699 /// getValueInit - Return the initializer for a value with the specified name,
700 /// or throw an exception if the field does not exist.
702 Init *getValueInit(const std::string &FieldName) const;
704 /// getValueAsString - This method looks up the specified field and returns
705 /// its value as a string, throwing an exception if the field does not exist
706 /// or if the value is not a string.
708 std::string getValueAsString(const std::string &FieldName) const;
710 /// getValueAsBitsInit - This method looks up the specified field and returns
711 /// its value as a BitsInit, throwing an exception if the field does not exist
712 /// or if the value is not the right type.
714 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
716 /// getValueAsListInit - This method looks up the specified field and returns
717 /// its value as a ListInit, throwing an exception if the field does not exist
718 /// or if the value is not the right type.
720 ListInit *getValueAsListInit(const std::string &FieldName) const;
722 /// getValueAsDef - This method looks up the specified field and returns its
723 /// value as a Record, throwing an exception if the field does not exist or if
724 /// the value is not the right type.
726 Record *getValueAsDef(const std::string &FieldName) const;
728 /// getValueAsBit - This method looks up the specified field and returns its
729 /// value as a bit, throwing an exception if the field does not exist or if
730 /// the value is not the right type.
732 bool getValueAsBit(const std::string &FieldName) const;
734 /// getValueAsInt - This method looks up the specified field and returns its
735 /// value as an int, throwing an exception if the field does not exist or if
736 /// the value is not the right type.
738 int getValueAsInt(const std::string &FieldName) const;
741 std::ostream &operator<<(std::ostream &OS, const Record &R);
744 std::map<std::string, Record*> Classes, Defs;
747 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
748 E = Classes.end(); I != E; ++I)
750 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
751 E = Defs.end(); I != E; ++I)
755 const std::map<std::string, Record*> &getClasses() const { return Classes; }
756 const std::map<std::string, Record*> &getDefs() const { return Defs; }
758 Record *getClass(const std::string &Name) const {
759 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
760 return I == Classes.end() ? 0 : I->second;
762 Record *getDef(const std::string &Name) const {
763 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
764 return I == Defs.end() ? 0 : I->second;
766 void addClass(Record *R) {
767 assert(getClass(R->getName()) == 0 && "Class already exists!");
768 Classes.insert(std::make_pair(R->getName(), R));
770 void addDef(Record *R) {
771 assert(getDef(R->getName()) == 0 && "Def already exists!");
772 Defs.insert(std::make_pair(R->getName(), R));
775 //===--------------------------------------------------------------------===//
776 // High-level helper methods, useful for tablegen backends...
778 /// getAllDerivedDefinitions - This method returns all concrete definitions
779 /// that derive from the specified class name. If a class with the specified
780 /// name does not exist, an exception is thrown.
782 getAllDerivedDefinitions(const std::string &ClassName) const;
788 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
790 extern RecordKeeper Records;