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...
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
51 virtual void print(std::ostream &OS) const = 0;
54 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
55 /// converted to the specified type.
56 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
58 public: // These methods should only be called from subclasses of Init
59 virtual Init *convertValue( UnsetInit *UI) { return 0; }
60 virtual Init *convertValue( BitInit *BI) { return 0; }
61 virtual Init *convertValue( BitsInit *BI) { return 0; }
62 virtual Init *convertValue( IntInit *II) { return 0; }
63 virtual Init *convertValue(StringInit *SI) { return 0; }
64 virtual Init *convertValue( ListInit *LI) { return 0; }
65 virtual Init *convertValue( CodeInit *CI) { return 0; }
66 virtual Init *convertValue(VarBitInit *VB) { return 0; }
67 virtual Init *convertValue( DefInit *DI) { return 0; }
68 virtual Init *convertValue( TypedInit *TI) { return 0; }
69 virtual Init *convertValue( VarInit *VI) {
70 return convertValue((TypedInit*)VI);
72 virtual Init *convertValue( FieldInit *FI) {
73 return convertValue((TypedInit*)FI);
76 public: // These methods should only be called by subclasses of RecTy.
77 // baseClassOf - These virtual methods should be overloaded to return true iff
78 // all values of type 'RHS' can be converted to the 'this' type.
79 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
80 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
81 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
82 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
83 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
84 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
85 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
88 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
94 /// BitRecTy - 'bit' - Represent a single bit
96 struct BitRecTy : public RecTy {
97 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
98 Init *convertValue(BitInit *BI) { return (Init*)BI; }
99 Init *convertValue(BitsInit *BI);
100 Init *convertValue(IntInit *II);
101 Init *convertValue(TypedInit *VI);
102 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
104 void print(std::ostream &OS) const { OS << "bit"; }
106 bool typeIsConvertibleTo(const RecTy *RHS) const {
107 return RHS->baseClassOf(this);
109 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
110 virtual bool baseClassOf(const BitsRecTy *RHS) const;
111 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
115 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
117 class BitsRecTy : public RecTy {
120 BitsRecTy(unsigned Sz) : Size(Sz) {}
122 unsigned getNumBits() const { return Size; }
124 Init *convertValue(UnsetInit *UI);
125 Init *convertValue(BitInit *UI);
126 Init *convertValue(BitsInit *BI);
127 Init *convertValue(IntInit *II);
128 Init *convertValue(TypedInit *VI);
130 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
132 bool typeIsConvertibleTo(const RecTy *RHS) const {
133 return RHS->baseClassOf(this);
135 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
136 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
137 virtual bool baseClassOf(const BitsRecTy *RHS) const {
138 return RHS->Size == Size;
143 /// IntRecTy - 'int' - Represent an integer value of no particular size
145 struct IntRecTy : public RecTy {
146 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
147 Init *convertValue(IntInit *II) { return (Init*)II; }
148 Init *convertValue(BitsInit *BI);
149 Init *convertValue(TypedInit *TI);
151 void print(std::ostream &OS) const { OS << "int"; }
153 bool typeIsConvertibleTo(const RecTy *RHS) const {
154 return RHS->baseClassOf(this);
157 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
158 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
159 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
162 /// StringRecTy - 'string' - Represent an string value
164 struct StringRecTy : public RecTy {
165 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
166 Init *convertValue(StringInit *SI) { return (Init*)SI; }
167 Init *convertValue(TypedInit *TI);
168 void print(std::ostream &OS) const { OS << "string"; }
170 bool typeIsConvertibleTo(const RecTy *RHS) const {
171 return RHS->baseClassOf(this);
174 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
177 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
178 /// the specified type.
180 class ListRecTy : public RecTy {
183 ListRecTy(RecTy *T) : Ty(T) {}
185 RecTy *getElementType() const { return Ty; }
187 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
188 Init *convertValue(ListInit *LI);
189 Init *convertValue(TypedInit *TI);
191 void print(std::ostream &OS) const;
193 bool typeIsConvertibleTo(const RecTy *RHS) const {
194 return RHS->baseClassOf(this);
197 virtual bool baseClassOf(const ListRecTy *RHS) const {
198 return RHS->getElementType()->typeIsConvertibleTo(Ty);
202 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
204 struct CodeRecTy : public RecTy {
205 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
206 Init *convertValue( CodeInit *CI) { return (Init*)CI; }
208 void print(std::ostream &OS) const { OS << "code"; }
210 bool typeIsConvertibleTo(const RecTy *RHS) const {
211 return RHS->baseClassOf(this);
213 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
217 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
220 class RecordRecTy : public RecTy {
223 RecordRecTy(Record *R) : Rec(R) {}
225 Record *getRecord() const { return Rec; }
227 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
228 Init *convertValue( DefInit *DI);
229 Init *convertValue(TypedInit *VI);
231 void print(std::ostream &OS) const;
233 bool typeIsConvertibleTo(const RecTy *RHS) const {
234 return RHS->baseClassOf(this);
236 virtual bool baseClassOf(const RecordRecTy *RHS) const;
241 //===----------------------------------------------------------------------===//
242 // Initializer Classes
243 //===----------------------------------------------------------------------===//
248 /// isComplete - This virtual method should be overridden by values that may
249 /// not be completely specified yet.
250 virtual bool isComplete() const { return true; }
252 /// print - Print out this value.
253 virtual void print(std::ostream &OS) const = 0;
255 /// dump - Debugging method that may be called through a debugger, just
256 /// invokes print on cerr.
259 /// convertInitializerTo - This virtual function is a simple call-back
260 /// function that should be overridden to call the appropriate
261 /// RecTy::convertValue method.
263 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
265 /// convertInitializerBitRange - This method is used to implement the bitrange
266 /// selection operator. Given an initializer, it selects the specified bits
267 /// out, returning them as a new init of bits type. If it is not legal to use
268 /// the bit subscript operator on this initializer, return null.
270 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
274 /// getFieldType - This method is used to implement the FieldInit class.
275 /// Implementors of this method should return the type of the named field if
276 /// they are of record type.
278 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
280 /// getFieldInit - This method complements getFieldType to return the
281 /// initializer for the specified field. If getFieldType returns non-null
282 /// this method should return non-null, otherwise it returns null.
284 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
288 /// resolveReferences - This method is used by classes that refer to other
289 /// variables which may not be defined at the time they expression is formed.
290 /// If a value is set for the variable later, this method will be called on
291 /// users of the value to allow the value to propagate out.
293 virtual Init *resolveReferences(Record &R) { return this; }
296 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
297 I.print(OS); return OS;
301 /// UnsetInit - ? - Represents an uninitialized value
303 struct UnsetInit : public Init {
304 virtual Init *convertInitializerTo(RecTy *Ty) {
305 return Ty->convertValue(this);
308 virtual bool isComplete() const { return false; }
309 virtual void print(std::ostream &OS) const { OS << "?"; }
313 /// BitInit - true/false - Represent a concrete initializer for a bit.
315 class BitInit : public Init {
318 BitInit(bool V) : Value(V) {}
320 bool getValue() const { return Value; }
322 virtual Init *convertInitializerTo(RecTy *Ty) {
323 return Ty->convertValue(this);
326 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
329 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
330 /// It contains a vector of bits, whose size is determined by the type.
332 class BitsInit : public Init {
333 std::vector<Init*> Bits;
335 BitsInit(unsigned Size) : Bits(Size) {}
337 unsigned getNumBits() const { return Bits.size(); }
339 Init *getBit(unsigned Bit) const {
340 assert(Bit < Bits.size() && "Bit index out of range!");
343 void setBit(unsigned Bit, Init *V) {
344 assert(Bit < Bits.size() && "Bit index out of range!");
345 assert(Bits[Bit] == 0 && "Bit already set!");
349 virtual Init *convertInitializerTo(RecTy *Ty) {
350 return Ty->convertValue(this);
352 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
354 virtual bool isComplete() const {
355 for (unsigned i = 0; i != getNumBits(); ++i)
356 if (!getBit(i)->isComplete()) return false;
359 virtual void print(std::ostream &OS) const;
361 virtual Init *resolveReferences(Record &R);
363 // printXX - Print this bitstream with the specified format, returning true if
364 // it is not possible.
365 bool printInHex(std::ostream &OS) const;
366 bool printAsVariable(std::ostream &OS) const;
367 bool printAsUnset(std::ostream &OS) const;
371 /// IntInit - 7 - Represent an initalization by a literal integer value.
373 class IntInit : public Init {
376 IntInit(int V) : Value(V) {}
378 int getValue() const { return Value; }
380 virtual Init *convertInitializerTo(RecTy *Ty) {
381 return Ty->convertValue(this);
383 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
385 virtual void print(std::ostream &OS) const { OS << Value; }
389 /// StringInit - "foo" - Represent an initialization by a string value.
391 class StringInit : public Init {
394 StringInit(const std::string &V) : Value(V) {}
396 const std::string &getValue() const { return Value; }
398 virtual Init *convertInitializerTo(RecTy *Ty) {
399 return Ty->convertValue(this);
402 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
405 /// CodeInit - "[{...}]" - Represent a code fragment.
407 class CodeInit : public Init {
410 CodeInit(const std::string &V) : Value(V) {}
412 const std::string getValue() const { return Value; }
414 virtual Init *convertInitializerTo(RecTy *Ty) {
415 return Ty->convertValue(this);
418 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
421 /// ListInit - [AL, AH, CL] - Represent a list of defs
423 class ListInit : public Init {
424 std::vector<Init*> Values;
426 ListInit(std::vector<Init*> &Vs) {
430 unsigned getSize() const { return Values.size(); }
431 Init *getElement(unsigned i) const {
432 assert(i < Values.size() && "List element index out of range!");
436 virtual Init *convertInitializerTo(RecTy *Ty) {
437 return Ty->convertValue(this);
440 virtual void print(std::ostream &OS) const;
444 /// TypedInit - This is the common super-class of types that have a specific,
447 class TypedInit : public Init {
450 TypedInit(RecTy *T) : Ty(T) {}
452 RecTy *getType() const { return Ty; }
454 /// resolveBitReference - This method is used to implement
455 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
456 /// simply return the resolved value, otherwise we return this.
458 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
461 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
463 class VarInit : public TypedInit {
466 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
468 virtual Init *convertInitializerTo(RecTy *Ty) {
469 return Ty->convertValue(this);
472 const std::string &getName() const { return VarName; }
474 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
476 virtual Init *resolveBitReference(Record &R, unsigned Bit);
478 virtual RecTy *getFieldType(const std::string &FieldName) const;
479 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
481 /// resolveReferences - This method is used by classes that refer to other
482 /// variables which may not be defined at the time they expression is formed.
483 /// If a value is set for the variable later, this method will be called on
484 /// users of the value to allow the value to propagate out.
486 virtual Init *resolveReferences(Record &R);
488 virtual void print(std::ostream &OS) const { OS << VarName; }
492 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
494 class VarBitInit : public Init {
498 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
499 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
500 ((BitsRecTy*)T->getType())->getNumBits() > B &&
501 "Illegal VarBitInit expression!");
504 virtual Init *convertInitializerTo(RecTy *Ty) {
505 return Ty->convertValue(this);
508 TypedInit *getVariable() const { return TI; }
509 unsigned getBitNum() const { return Bit; }
511 virtual void print(std::ostream &OS) const {
512 TI->print(OS); OS << "{" << Bit << "}";
514 virtual Init *resolveReferences(Record &R);
518 /// DefInit - AL - Represent a reference to a 'def' in the description
520 class DefInit : public Init {
523 DefInit(Record *D) : Def(D) {}
525 virtual Init *convertInitializerTo(RecTy *Ty) {
526 return Ty->convertValue(this);
529 Record *getDef() const { return Def; }
531 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
533 virtual RecTy *getFieldType(const std::string &FieldName) const;
534 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
536 virtual void print(std::ostream &OS) const;
540 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
542 class FieldInit : public TypedInit {
543 Init *Rec; // Record we are referring to
544 std::string FieldName; // Field we are accessing
546 FieldInit(Init *R, const std::string &FN)
547 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
548 assert(getType() && "FieldInit with non-record type!");
551 virtual Init *convertInitializerTo(RecTy *Ty) {
552 return Ty->convertValue(this);
555 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
557 virtual Init *resolveBitReference(Record &R, unsigned Bit);
559 virtual Init *resolveReferences(Record &R);
561 virtual void print(std::ostream &OS) const {
562 Rec->print(OS); OS << "." << FieldName;
567 //===----------------------------------------------------------------------===//
568 // High-Level Classes
569 //===----------------------------------------------------------------------===//
577 RecordVal(const std::string &N, RecTy *T, unsigned P);
579 const std::string &getName() const { return Name; }
581 unsigned getPrefix() const { return Prefix; }
582 RecTy *getType() const { return Ty; }
583 Init *getValue() const { return Value; }
585 bool setValue(Init *V) {
587 Value = V->convertInitializerTo(Ty);
595 void print(std::ostream &OS, bool PrintSem = true) const;
598 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
604 const std::string Name;
605 std::vector<std::string> TemplateArgs;
606 std::vector<RecordVal> Values;
607 std::vector<Record*> SuperClasses;
610 Record(const std::string &N) : Name(N) {}
613 const std::string &getName() const { return Name; }
614 const std::vector<std::string> &getTemplateArgs() const {
617 const std::vector<RecordVal> &getValues() const { return Values; }
618 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
620 bool isTemplateArg(const std::string &Name) const {
621 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
622 if (TemplateArgs[i] == Name) return true;
626 const RecordVal *getValue(const std::string &Name) const {
627 for (unsigned i = 0, e = Values.size(); i != e; ++i)
628 if (Values[i].getName() == Name) return &Values[i];
631 RecordVal *getValue(const std::string &Name) {
632 for (unsigned i = 0, e = Values.size(); i != e; ++i)
633 if (Values[i].getName() == Name) return &Values[i];
637 void addTemplateArg(const std::string &Name) {
638 assert(!isTemplateArg(Name) && "Template arg already defined!");
639 TemplateArgs.push_back(Name);
642 void addValue(const RecordVal &RV) {
643 assert(getValue(RV.getName()) == 0 && "Value already added!");
644 Values.push_back(RV);
647 void removeValue(const std::string &Name) {
648 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
649 for (unsigned i = 0, e = Values.size(); i != e; ++i)
650 if (Values[i].getName() == Name) {
651 Values.erase(Values.begin()+i);
654 assert(0 && "Name does not exist in record!");
657 bool isSubClassOf(Record *R) const {
658 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
659 if (SuperClasses[i] == R)
664 void addSuperClass(Record *R) {
665 assert(!isSubClassOf(R) && "Already subclassing record!");
666 SuperClasses.push_back(R);
669 // resolveReferences - If there are any field references that refer to fields
670 // that have been filled in, we can propagate the values now.
672 void resolveReferences();
676 //===--------------------------------------------------------------------===//
677 // High-level methods useful to tablegen back-ends
680 /// getValueInit - Return the initializer for a value with the specified name,
681 /// or throw an exception if the field does not exist.
683 Init *getValueInit(const std::string &FieldName) const;
685 /// getValueAsString - This method looks up the specified field and returns
686 /// its value as a string, throwing an exception if the field does not exist
687 /// or if the value is not a string.
689 std::string getValueAsString(const std::string &FieldName) const;
691 /// getValueAsBitsInit - This method looks up the specified field and returns
692 /// its value as a BitsInit, throwing an exception if the field does not exist
693 /// or if the value is not the right type.
695 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
697 /// getValueAsListInit - This method looks up the specified field and returns
698 /// its value as a ListInit, throwing an exception if the field does not exist
699 /// or if the value is not the right type.
701 ListInit *getValueAsListInit(const std::string &FieldName) const;
703 /// getValueAsDef - This method looks up the specified field and returns its
704 /// value as a Record, throwing an exception if the field does not exist or if
705 /// the value is not the right type.
707 Record *getValueAsDef(const std::string &FieldName) const;
709 /// getValueAsInt - This method looks up the specified field and returns its
710 /// value as an int, throwing an exception if the field does not exist or if
711 /// the value is not the right type.
713 int getValueAsInt(const std::string &FieldName) const;
716 std::ostream &operator<<(std::ostream &OS, const Record &R);
719 std::map<std::string, Record*> Classes, Defs;
722 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
723 E = Classes.end(); I != E; ++I)
725 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
726 E = Defs.end(); I != E; ++I)
730 const std::map<std::string, Record*> &getClasses() const { return Classes; }
731 const std::map<std::string, Record*> &getDefs() const { return Defs; }
733 Record *getClass(const std::string &Name) const {
734 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
735 return I == Classes.end() ? 0 : I->second;
737 Record *getDef(const std::string &Name) const {
738 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
739 return I == Defs.end() ? 0 : I->second;
741 void addClass(Record *R) {
742 assert(getClass(R->getName()) == 0 && "Class already exists!");
743 Classes.insert(std::make_pair(R->getName(), R));
745 void addDef(Record *R) {
746 assert(getDef(R->getName()) == 0 && "Def already exists!");
747 Defs.insert(std::make_pair(R->getName(), R));
750 //===--------------------------------------------------------------------===//
751 // High-level helper methods, useful for tablegen backends...
753 /// getAllDerivedDefinitions - This method returns all concrete definitions
754 /// that derive from the specified class name. If a class with the specified
755 /// name does not exist, an exception is thrown.
757 getAllDerivedDefinitions(const std::string &ClassName) const;
763 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
765 extern RecordKeeper Records;