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...
46 //===----------------------------------------------------------------------===//
48 //===----------------------------------------------------------------------===//
53 virtual void print(std::ostream &OS) const = 0;
56 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
57 /// converted to the specified type.
58 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
60 public: // These methods should only be called from subclasses of Init
61 virtual Init *convertValue( UnsetInit *UI) { return 0; }
62 virtual Init *convertValue( BitInit *BI) { return 0; }
63 virtual Init *convertValue( BitsInit *BI) { return 0; }
64 virtual Init *convertValue( IntInit *II) { return 0; }
65 virtual Init *convertValue(StringInit *SI) { return 0; }
66 virtual Init *convertValue( ListInit *LI) { return 0; }
67 virtual Init *convertValue( CodeInit *CI) { return 0; }
68 virtual Init *convertValue(VarBitInit *VB) { return 0; }
69 virtual Init *convertValue( DefInit *DI) { return 0; }
70 virtual Init *convertValue( DagInit *DI) { return 0; }
71 virtual Init *convertValue( TypedInit *TI) { return 0; }
72 virtual Init *convertValue( VarInit *VI) {
73 return convertValue((TypedInit*)VI);
75 virtual Init *convertValue( FieldInit *FI) {
76 return convertValue((TypedInit*)FI);
79 public: // These methods should only be called by subclasses of RecTy.
80 // baseClassOf - These virtual methods should be overloaded to return true iff
81 // all values of type 'RHS' can be converted to the 'this' type.
82 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
83 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
84 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
85 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
86 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
87 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
88 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
89 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
92 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
98 /// BitRecTy - 'bit' - Represent a single bit
100 struct BitRecTy : public RecTy {
101 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
102 Init *convertValue(BitInit *BI) { return (Init*)BI; }
103 Init *convertValue(BitsInit *BI);
104 Init *convertValue(IntInit *II);
105 Init *convertValue(TypedInit *VI);
106 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
108 void print(std::ostream &OS) const { OS << "bit"; }
110 bool typeIsConvertibleTo(const RecTy *RHS) const {
111 return RHS->baseClassOf(this);
113 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
114 virtual bool baseClassOf(const BitsRecTy *RHS) const;
115 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
119 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
121 class BitsRecTy : public RecTy {
124 BitsRecTy(unsigned Sz) : Size(Sz) {}
126 unsigned getNumBits() const { return Size; }
128 Init *convertValue(UnsetInit *UI);
129 Init *convertValue(BitInit *UI);
130 Init *convertValue(BitsInit *BI);
131 Init *convertValue(IntInit *II);
132 Init *convertValue(TypedInit *VI);
134 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
136 bool typeIsConvertibleTo(const RecTy *RHS) const {
137 return RHS->baseClassOf(this);
139 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
140 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
141 virtual bool baseClassOf(const BitsRecTy *RHS) const {
142 return RHS->Size == Size;
147 /// IntRecTy - 'int' - Represent an integer value of no particular size
149 struct IntRecTy : public RecTy {
150 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
151 Init *convertValue(IntInit *II) { return (Init*)II; }
152 Init *convertValue(BitInit *BI);
153 Init *convertValue(BitsInit *BI);
154 Init *convertValue(TypedInit *TI);
156 void print(std::ostream &OS) const { OS << "int"; }
158 bool typeIsConvertibleTo(const RecTy *RHS) const {
159 return RHS->baseClassOf(this);
162 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
163 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
164 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
167 /// StringRecTy - 'string' - Represent an string value
169 struct StringRecTy : public RecTy {
170 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
171 Init *convertValue(StringInit *SI) { return (Init*)SI; }
172 Init *convertValue(TypedInit *TI);
173 void print(std::ostream &OS) const { OS << "string"; }
175 bool typeIsConvertibleTo(const RecTy *RHS) const {
176 return RHS->baseClassOf(this);
179 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
182 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
183 /// the specified type.
185 class ListRecTy : public RecTy {
188 ListRecTy(RecTy *T) : Ty(T) {}
190 RecTy *getElementType() const { return Ty; }
192 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
193 Init *convertValue(ListInit *LI);
194 Init *convertValue(TypedInit *TI);
196 void print(std::ostream &OS) const;
198 bool typeIsConvertibleTo(const RecTy *RHS) const {
199 return RHS->baseClassOf(this);
202 virtual bool baseClassOf(const ListRecTy *RHS) const {
203 return RHS->getElementType()->typeIsConvertibleTo(Ty);
207 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
209 struct CodeRecTy : public RecTy {
210 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
211 Init *convertValue( CodeInit *CI) { return (Init*)CI; }
212 Init *convertValue(TypedInit *TI);
214 void print(std::ostream &OS) const { OS << "code"; }
216 bool typeIsConvertibleTo(const RecTy *RHS) const {
217 return RHS->baseClassOf(this);
219 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
222 /// DagRecTy - 'dag' - Represent a dag fragment
224 struct DagRecTy : public RecTy {
225 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
226 Init *convertValue( DagInit *CI) { return (Init*)CI; }
227 Init *convertValue(TypedInit *TI);
229 void print(std::ostream &OS) const { OS << "dag"; }
231 bool typeIsConvertibleTo(const RecTy *RHS) const {
232 return RHS->baseClassOf(this);
234 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
238 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
241 class RecordRecTy : public RecTy {
244 RecordRecTy(Record *R) : Rec(R) {}
246 Record *getRecord() const { return Rec; }
248 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
249 Init *convertValue( DefInit *DI);
250 Init *convertValue(TypedInit *VI);
252 void print(std::ostream &OS) const;
254 bool typeIsConvertibleTo(const RecTy *RHS) const {
255 return RHS->baseClassOf(this);
257 virtual bool baseClassOf(const RecordRecTy *RHS) const;
262 //===----------------------------------------------------------------------===//
263 // Initializer Classes
264 //===----------------------------------------------------------------------===//
269 /// isComplete - This virtual method should be overridden by values that may
270 /// not be completely specified yet.
271 virtual bool isComplete() const { return true; }
273 /// print - Print out this value.
274 virtual void print(std::ostream &OS) const = 0;
276 /// dump - Debugging method that may be called through a debugger, just
277 /// invokes print on cerr.
280 /// convertInitializerTo - This virtual function is a simple call-back
281 /// function that should be overridden to call the appropriate
282 /// RecTy::convertValue method.
284 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
286 /// convertInitializerBitRange - This method is used to implement the bitrange
287 /// selection operator. Given an initializer, it selects the specified bits
288 /// out, returning them as a new init of bits type. If it is not legal to use
289 /// the bit subscript operator on this initializer, return null.
291 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
295 /// getFieldType - This method is used to implement the FieldInit class.
296 /// Implementors of this method should return the type of the named field if
297 /// they are of record type.
299 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
301 /// getFieldInit - This method complements getFieldType to return the
302 /// initializer for the specified field. If getFieldType returns non-null
303 /// this method should return non-null, otherwise it returns null.
305 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
309 /// resolveReferences - This method is used by classes that refer to other
310 /// variables which may not be defined at the time they expression is formed.
311 /// If a value is set for the variable later, this method will be called on
312 /// users of the value to allow the value to propagate out.
314 virtual Init *resolveReferences(Record &R) { return this; }
317 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
318 I.print(OS); return OS;
322 /// UnsetInit - ? - Represents an uninitialized value
324 struct UnsetInit : public Init {
325 virtual Init *convertInitializerTo(RecTy *Ty) {
326 return Ty->convertValue(this);
329 virtual bool isComplete() const { return false; }
330 virtual void print(std::ostream &OS) const { OS << "?"; }
334 /// BitInit - true/false - Represent a concrete initializer for a bit.
336 class BitInit : public Init {
339 BitInit(bool V) : Value(V) {}
341 bool getValue() const { return Value; }
343 virtual Init *convertInitializerTo(RecTy *Ty) {
344 return Ty->convertValue(this);
347 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
350 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
351 /// It contains a vector of bits, whose size is determined by the type.
353 class BitsInit : public Init {
354 std::vector<Init*> Bits;
356 BitsInit(unsigned Size) : Bits(Size) {}
358 unsigned getNumBits() const { return Bits.size(); }
360 Init *getBit(unsigned Bit) const {
361 assert(Bit < Bits.size() && "Bit index out of range!");
364 void setBit(unsigned Bit, Init *V) {
365 assert(Bit < Bits.size() && "Bit index out of range!");
366 assert(Bits[Bit] == 0 && "Bit already set!");
370 virtual Init *convertInitializerTo(RecTy *Ty) {
371 return Ty->convertValue(this);
373 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
375 virtual bool isComplete() const {
376 for (unsigned i = 0; i != getNumBits(); ++i)
377 if (!getBit(i)->isComplete()) return false;
380 virtual void print(std::ostream &OS) const;
382 virtual Init *resolveReferences(Record &R);
384 // printXX - Print this bitstream with the specified format, returning true if
385 // it is not possible.
386 bool printInHex(std::ostream &OS) const;
387 bool printAsVariable(std::ostream &OS) const;
388 bool printAsUnset(std::ostream &OS) const;
392 /// IntInit - 7 - Represent an initalization by a literal integer value.
394 class IntInit : public Init {
397 IntInit(int V) : Value(V) {}
399 int getValue() const { return Value; }
401 virtual Init *convertInitializerTo(RecTy *Ty) {
402 return Ty->convertValue(this);
404 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
406 virtual void print(std::ostream &OS) const { OS << Value; }
410 /// StringInit - "foo" - Represent an initialization by a string value.
412 class StringInit : public Init {
415 StringInit(const std::string &V) : Value(V) {}
417 const std::string &getValue() const { return Value; }
419 virtual Init *convertInitializerTo(RecTy *Ty) {
420 return Ty->convertValue(this);
423 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
426 /// CodeInit - "[{...}]" - Represent a code fragment.
428 class CodeInit : public Init {
431 CodeInit(const std::string &V) : Value(V) {}
433 const std::string getValue() const { return Value; }
435 virtual Init *convertInitializerTo(RecTy *Ty) {
436 return Ty->convertValue(this);
439 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
442 /// ListInit - [AL, AH, CL] - Represent a list of defs
444 class ListInit : public Init {
445 std::vector<Init*> Values;
447 ListInit(std::vector<Init*> &Vs) {
451 unsigned getSize() const { return Values.size(); }
452 Init *getElement(unsigned i) const {
453 assert(i < Values.size() && "List element index out of range!");
457 virtual Init *convertInitializerTo(RecTy *Ty) {
458 return Ty->convertValue(this);
461 virtual void print(std::ostream &OS) const;
465 /// TypedInit - This is the common super-class of types that have a specific,
468 class TypedInit : public Init {
471 TypedInit(RecTy *T) : Ty(T) {}
473 RecTy *getType() const { return Ty; }
475 /// resolveBitReference - This method is used to implement
476 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
477 /// simply return the resolved value, otherwise we return this.
479 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
482 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
484 class VarInit : public TypedInit {
487 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
489 virtual Init *convertInitializerTo(RecTy *Ty) {
490 return Ty->convertValue(this);
493 const std::string &getName() const { return VarName; }
495 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
497 virtual Init *resolveBitReference(Record &R, unsigned Bit);
499 virtual RecTy *getFieldType(const std::string &FieldName) const;
500 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
502 /// resolveReferences - This method is used by classes that refer to other
503 /// variables which may not be defined at the time they expression is formed.
504 /// If a value is set for the variable later, this method will be called on
505 /// users of the value to allow the value to propagate out.
507 virtual Init *resolveReferences(Record &R);
509 virtual void print(std::ostream &OS) const { OS << VarName; }
513 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
515 class VarBitInit : public Init {
519 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
520 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
521 ((BitsRecTy*)T->getType())->getNumBits() > B &&
522 "Illegal VarBitInit expression!");
525 virtual Init *convertInitializerTo(RecTy *Ty) {
526 return Ty->convertValue(this);
529 TypedInit *getVariable() const { return TI; }
530 unsigned getBitNum() const { return Bit; }
532 virtual void print(std::ostream &OS) const {
533 TI->print(OS); OS << "{" << Bit << "}";
535 virtual Init *resolveReferences(Record &R);
539 /// DefInit - AL - Represent a reference to a 'def' in the description
541 class DefInit : public Init {
544 DefInit(Record *D) : Def(D) {}
546 virtual Init *convertInitializerTo(RecTy *Ty) {
547 return Ty->convertValue(this);
550 Record *getDef() const { return Def; }
552 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
554 virtual RecTy *getFieldType(const std::string &FieldName) const;
555 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
557 virtual void print(std::ostream &OS) const;
561 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
563 class FieldInit : public TypedInit {
564 Init *Rec; // Record we are referring to
565 std::string FieldName; // Field we are accessing
567 FieldInit(Init *R, const std::string &FN)
568 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
569 assert(getType() && "FieldInit with non-record type!");
572 virtual Init *convertInitializerTo(RecTy *Ty) {
573 return Ty->convertValue(this);
576 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
578 virtual Init *resolveBitReference(Record &R, unsigned Bit);
580 virtual Init *resolveReferences(Record &R);
582 virtual void print(std::ostream &OS) const {
583 Rec->print(OS); OS << "." << FieldName;
587 /// DagInit - (def a, b) - Represent a DAG tree value. DAG inits are required
588 /// to have Records for their first value, after that, any legal Init is
591 class DagInit : public Init {
593 std::vector<Init*> Args;
595 DagInit(Record *D, std::vector<Init*> &a) : NodeTypeDef(D) {
596 Args.swap(a); // DESTRUCTIVELY take the arguments
599 virtual Init *convertInitializerTo(RecTy *Ty) {
600 return Ty->convertValue(this);
603 Record *getNodeType() const { return NodeTypeDef; }
604 const std::vector<Init*> &getArgs() const { return Args; }
606 virtual void print(std::ostream &OS) const;
609 //===----------------------------------------------------------------------===//
610 // High-Level Classes
611 //===----------------------------------------------------------------------===//
619 RecordVal(const std::string &N, RecTy *T, unsigned P);
621 const std::string &getName() const { return Name; }
623 unsigned getPrefix() const { return Prefix; }
624 RecTy *getType() const { return Ty; }
625 Init *getValue() const { return Value; }
627 bool setValue(Init *V) {
629 Value = V->convertInitializerTo(Ty);
637 void print(std::ostream &OS, bool PrintSem = true) const;
640 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
646 const std::string Name;
647 std::vector<std::string> TemplateArgs;
648 std::vector<RecordVal> Values;
649 std::vector<Record*> SuperClasses;
652 Record(const std::string &N) : Name(N) {}
655 const std::string &getName() const { return Name; }
656 const std::vector<std::string> &getTemplateArgs() const {
659 const std::vector<RecordVal> &getValues() const { return Values; }
660 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
662 bool isTemplateArg(const std::string &Name) const {
663 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
664 if (TemplateArgs[i] == Name) return true;
668 const RecordVal *getValue(const std::string &Name) const {
669 for (unsigned i = 0, e = Values.size(); i != e; ++i)
670 if (Values[i].getName() == Name) return &Values[i];
673 RecordVal *getValue(const std::string &Name) {
674 for (unsigned i = 0, e = Values.size(); i != e; ++i)
675 if (Values[i].getName() == Name) return &Values[i];
679 void addTemplateArg(const std::string &Name) {
680 assert(!isTemplateArg(Name) && "Template arg already defined!");
681 TemplateArgs.push_back(Name);
684 void addValue(const RecordVal &RV) {
685 assert(getValue(RV.getName()) == 0 && "Value already added!");
686 Values.push_back(RV);
689 void removeValue(const std::string &Name) {
690 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
691 for (unsigned i = 0, e = Values.size(); i != e; ++i)
692 if (Values[i].getName() == Name) {
693 Values.erase(Values.begin()+i);
696 assert(0 && "Name does not exist in record!");
699 bool isSubClassOf(Record *R) const {
700 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
701 if (SuperClasses[i] == R)
706 void addSuperClass(Record *R) {
707 assert(!isSubClassOf(R) && "Already subclassing record!");
708 SuperClasses.push_back(R);
711 // resolveReferences - If there are any field references that refer to fields
712 // that have been filled in, we can propagate the values now.
714 void resolveReferences();
718 //===--------------------------------------------------------------------===//
719 // High-level methods useful to tablegen back-ends
722 /// getValueInit - Return the initializer for a value with the specified name,
723 /// or throw an exception if the field does not exist.
725 Init *getValueInit(const std::string &FieldName) const;
727 /// getValueAsString - This method looks up the specified field and returns
728 /// its value as a string, throwing an exception if the field does not exist
729 /// or if the value is not a string.
731 std::string getValueAsString(const std::string &FieldName) const;
733 /// getValueAsBitsInit - This method looks up the specified field and returns
734 /// its value as a BitsInit, throwing an exception if the field does not exist
735 /// or if the value is not the right type.
737 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
739 /// getValueAsListInit - This method looks up the specified field and returns
740 /// its value as a ListInit, throwing an exception if the field does not exist
741 /// or if the value is not the right type.
743 ListInit *getValueAsListInit(const std::string &FieldName) const;
745 /// getValueAsDef - This method looks up the specified field and returns its
746 /// value as a Record, throwing an exception if the field does not exist or if
747 /// the value is not the right type.
749 Record *getValueAsDef(const std::string &FieldName) const;
751 /// getValueAsBit - This method looks up the specified field and returns its
752 /// value as a bit, throwing an exception if the field does not exist or if
753 /// the value is not the right type.
755 bool getValueAsBit(const std::string &FieldName) const;
757 /// getValueAsInt - This method looks up the specified field and returns its
758 /// value as an int, throwing an exception if the field does not exist or if
759 /// the value is not the right type.
761 int getValueAsInt(const std::string &FieldName) const;
763 /// getValueAsDag - This method looks up the specified field and returns its
764 /// value as an Dag, throwing an exception if the field does not exist or if
765 /// the value is not the right type.
767 DagInit *getValueAsDag(const std::string &FieldName) const;
770 std::ostream &operator<<(std::ostream &OS, const Record &R);
773 std::map<std::string, Record*> Classes, Defs;
776 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
777 E = Classes.end(); I != E; ++I)
779 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
780 E = Defs.end(); I != E; ++I)
784 const std::map<std::string, Record*> &getClasses() const { return Classes; }
785 const std::map<std::string, Record*> &getDefs() const { return Defs; }
787 Record *getClass(const std::string &Name) const {
788 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
789 return I == Classes.end() ? 0 : I->second;
791 Record *getDef(const std::string &Name) const {
792 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
793 return I == Defs.end() ? 0 : I->second;
795 void addClass(Record *R) {
796 assert(getClass(R->getName()) == 0 && "Class already exists!");
797 Classes.insert(std::make_pair(R->getName(), R));
799 void addDef(Record *R) {
800 assert(getDef(R->getName()) == 0 && "Def already exists!");
801 Defs.insert(std::make_pair(R->getName(), R));
804 //===--------------------------------------------------------------------===//
805 // High-level helper methods, useful for tablegen backends...
807 /// getAllDerivedDefinitions - This method returns all concrete definitions
808 /// that derive from the specified class name. If a class with the specified
809 /// name does not exist, an exception is thrown.
811 getAllDerivedDefinitions(const std::string &ClassName) const;
817 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
819 extern RecordKeeper Records;