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(BitInit *BI);
149 Init *convertValue(BitsInit *BI);
150 Init *convertValue(TypedInit *TI);
152 void print(std::ostream &OS) const { OS << "int"; }
154 bool typeIsConvertibleTo(const RecTy *RHS) const {
155 return RHS->baseClassOf(this);
158 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
159 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
160 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
163 /// StringRecTy - 'string' - Represent an string value
165 struct StringRecTy : public RecTy {
166 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
167 Init *convertValue(StringInit *SI) { return (Init*)SI; }
168 Init *convertValue(TypedInit *TI);
169 void print(std::ostream &OS) const { OS << "string"; }
171 bool typeIsConvertibleTo(const RecTy *RHS) const {
172 return RHS->baseClassOf(this);
175 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
178 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
179 /// the specified type.
181 class ListRecTy : public RecTy {
184 ListRecTy(RecTy *T) : Ty(T) {}
186 RecTy *getElementType() const { return Ty; }
188 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
189 Init *convertValue(ListInit *LI);
190 Init *convertValue(TypedInit *TI);
192 void print(std::ostream &OS) const;
194 bool typeIsConvertibleTo(const RecTy *RHS) const {
195 return RHS->baseClassOf(this);
198 virtual bool baseClassOf(const ListRecTy *RHS) const {
199 return RHS->getElementType()->typeIsConvertibleTo(Ty);
203 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
205 struct CodeRecTy : public RecTy {
206 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
207 Init *convertValue( CodeInit *CI) { return (Init*)CI; }
209 void print(std::ostream &OS) const { OS << "code"; }
211 bool typeIsConvertibleTo(const RecTy *RHS) const {
212 return RHS->baseClassOf(this);
214 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
218 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
221 class RecordRecTy : public RecTy {
224 RecordRecTy(Record *R) : Rec(R) {}
226 Record *getRecord() const { return Rec; }
228 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
229 Init *convertValue( DefInit *DI);
230 Init *convertValue(TypedInit *VI);
232 void print(std::ostream &OS) const;
234 bool typeIsConvertibleTo(const RecTy *RHS) const {
235 return RHS->baseClassOf(this);
237 virtual bool baseClassOf(const RecordRecTy *RHS) const;
242 //===----------------------------------------------------------------------===//
243 // Initializer Classes
244 //===----------------------------------------------------------------------===//
249 /// isComplete - This virtual method should be overridden by values that may
250 /// not be completely specified yet.
251 virtual bool isComplete() const { return true; }
253 /// print - Print out this value.
254 virtual void print(std::ostream &OS) const = 0;
256 /// dump - Debugging method that may be called through a debugger, just
257 /// invokes print on cerr.
260 /// convertInitializerTo - This virtual function is a simple call-back
261 /// function that should be overridden to call the appropriate
262 /// RecTy::convertValue method.
264 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
266 /// convertInitializerBitRange - This method is used to implement the bitrange
267 /// selection operator. Given an initializer, it selects the specified bits
268 /// out, returning them as a new init of bits type. If it is not legal to use
269 /// the bit subscript operator on this initializer, return null.
271 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
275 /// getFieldType - This method is used to implement the FieldInit class.
276 /// Implementors of this method should return the type of the named field if
277 /// they are of record type.
279 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
281 /// getFieldInit - This method complements getFieldType to return the
282 /// initializer for the specified field. If getFieldType returns non-null
283 /// this method should return non-null, otherwise it returns null.
285 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
289 /// resolveReferences - This method is used by classes that refer to other
290 /// variables which may not be defined at the time they expression is formed.
291 /// If a value is set for the variable later, this method will be called on
292 /// users of the value to allow the value to propagate out.
294 virtual Init *resolveReferences(Record &R) { return this; }
297 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
298 I.print(OS); return OS;
302 /// UnsetInit - ? - Represents an uninitialized value
304 struct UnsetInit : public Init {
305 virtual Init *convertInitializerTo(RecTy *Ty) {
306 return Ty->convertValue(this);
309 virtual bool isComplete() const { return false; }
310 virtual void print(std::ostream &OS) const { OS << "?"; }
314 /// BitInit - true/false - Represent a concrete initializer for a bit.
316 class BitInit : public Init {
319 BitInit(bool V) : Value(V) {}
321 bool getValue() const { return Value; }
323 virtual Init *convertInitializerTo(RecTy *Ty) {
324 return Ty->convertValue(this);
327 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
330 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
331 /// It contains a vector of bits, whose size is determined by the type.
333 class BitsInit : public Init {
334 std::vector<Init*> Bits;
336 BitsInit(unsigned Size) : Bits(Size) {}
338 unsigned getNumBits() const { return Bits.size(); }
340 Init *getBit(unsigned Bit) const {
341 assert(Bit < Bits.size() && "Bit index out of range!");
344 void setBit(unsigned Bit, Init *V) {
345 assert(Bit < Bits.size() && "Bit index out of range!");
346 assert(Bits[Bit] == 0 && "Bit already set!");
350 virtual Init *convertInitializerTo(RecTy *Ty) {
351 return Ty->convertValue(this);
353 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
355 virtual bool isComplete() const {
356 for (unsigned i = 0; i != getNumBits(); ++i)
357 if (!getBit(i)->isComplete()) return false;
360 virtual void print(std::ostream &OS) const;
362 virtual Init *resolveReferences(Record &R);
364 // printXX - Print this bitstream with the specified format, returning true if
365 // it is not possible.
366 bool printInHex(std::ostream &OS) const;
367 bool printAsVariable(std::ostream &OS) const;
368 bool printAsUnset(std::ostream &OS) const;
372 /// IntInit - 7 - Represent an initalization by a literal integer value.
374 class IntInit : public Init {
377 IntInit(int V) : Value(V) {}
379 int getValue() const { return Value; }
381 virtual Init *convertInitializerTo(RecTy *Ty) {
382 return Ty->convertValue(this);
384 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
386 virtual void print(std::ostream &OS) const { OS << Value; }
390 /// StringInit - "foo" - Represent an initialization by a string value.
392 class StringInit : public Init {
395 StringInit(const std::string &V) : Value(V) {}
397 const std::string &getValue() const { return Value; }
399 virtual Init *convertInitializerTo(RecTy *Ty) {
400 return Ty->convertValue(this);
403 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
406 /// CodeInit - "[{...}]" - Represent a code fragment.
408 class CodeInit : public Init {
411 CodeInit(const std::string &V) : Value(V) {}
413 const std::string getValue() const { return Value; }
415 virtual Init *convertInitializerTo(RecTy *Ty) {
416 return Ty->convertValue(this);
419 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
422 /// ListInit - [AL, AH, CL] - Represent a list of defs
424 class ListInit : public Init {
425 std::vector<Init*> Values;
427 ListInit(std::vector<Init*> &Vs) {
431 unsigned getSize() const { return Values.size(); }
432 Init *getElement(unsigned i) const {
433 assert(i < Values.size() && "List element index out of range!");
437 virtual Init *convertInitializerTo(RecTy *Ty) {
438 return Ty->convertValue(this);
441 virtual void print(std::ostream &OS) const;
445 /// TypedInit - This is the common super-class of types that have a specific,
448 class TypedInit : public Init {
451 TypedInit(RecTy *T) : Ty(T) {}
453 RecTy *getType() const { return Ty; }
455 /// resolveBitReference - This method is used to implement
456 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
457 /// simply return the resolved value, otherwise we return this.
459 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
462 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
464 class VarInit : public TypedInit {
467 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
469 virtual Init *convertInitializerTo(RecTy *Ty) {
470 return Ty->convertValue(this);
473 const std::string &getName() const { return VarName; }
475 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
477 virtual Init *resolveBitReference(Record &R, unsigned Bit);
479 virtual RecTy *getFieldType(const std::string &FieldName) const;
480 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
482 /// resolveReferences - This method is used by classes that refer to other
483 /// variables which may not be defined at the time they expression is formed.
484 /// If a value is set for the variable later, this method will be called on
485 /// users of the value to allow the value to propagate out.
487 virtual Init *resolveReferences(Record &R);
489 virtual void print(std::ostream &OS) const { OS << VarName; }
493 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
495 class VarBitInit : public Init {
499 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
500 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
501 ((BitsRecTy*)T->getType())->getNumBits() > B &&
502 "Illegal VarBitInit expression!");
505 virtual Init *convertInitializerTo(RecTy *Ty) {
506 return Ty->convertValue(this);
509 TypedInit *getVariable() const { return TI; }
510 unsigned getBitNum() const { return Bit; }
512 virtual void print(std::ostream &OS) const {
513 TI->print(OS); OS << "{" << Bit << "}";
515 virtual Init *resolveReferences(Record &R);
519 /// DefInit - AL - Represent a reference to a 'def' in the description
521 class DefInit : public Init {
524 DefInit(Record *D) : Def(D) {}
526 virtual Init *convertInitializerTo(RecTy *Ty) {
527 return Ty->convertValue(this);
530 Record *getDef() const { return Def; }
532 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
534 virtual RecTy *getFieldType(const std::string &FieldName) const;
535 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
537 virtual void print(std::ostream &OS) const;
541 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
543 class FieldInit : public TypedInit {
544 Init *Rec; // Record we are referring to
545 std::string FieldName; // Field we are accessing
547 FieldInit(Init *R, const std::string &FN)
548 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
549 assert(getType() && "FieldInit with non-record type!");
552 virtual Init *convertInitializerTo(RecTy *Ty) {
553 return Ty->convertValue(this);
556 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
558 virtual Init *resolveBitReference(Record &R, unsigned Bit);
560 virtual Init *resolveReferences(Record &R);
562 virtual void print(std::ostream &OS) const {
563 Rec->print(OS); OS << "." << FieldName;
568 //===----------------------------------------------------------------------===//
569 // High-Level Classes
570 //===----------------------------------------------------------------------===//
578 RecordVal(const std::string &N, RecTy *T, unsigned P);
580 const std::string &getName() const { return Name; }
582 unsigned getPrefix() const { return Prefix; }
583 RecTy *getType() const { return Ty; }
584 Init *getValue() const { return Value; }
586 bool setValue(Init *V) {
588 Value = V->convertInitializerTo(Ty);
596 void print(std::ostream &OS, bool PrintSem = true) const;
599 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
605 const std::string Name;
606 std::vector<std::string> TemplateArgs;
607 std::vector<RecordVal> Values;
608 std::vector<Record*> SuperClasses;
611 Record(const std::string &N) : Name(N) {}
614 const std::string &getName() const { return Name; }
615 const std::vector<std::string> &getTemplateArgs() const {
618 const std::vector<RecordVal> &getValues() const { return Values; }
619 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
621 bool isTemplateArg(const std::string &Name) const {
622 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
623 if (TemplateArgs[i] == Name) return true;
627 const RecordVal *getValue(const std::string &Name) const {
628 for (unsigned i = 0, e = Values.size(); i != e; ++i)
629 if (Values[i].getName() == Name) return &Values[i];
632 RecordVal *getValue(const std::string &Name) {
633 for (unsigned i = 0, e = Values.size(); i != e; ++i)
634 if (Values[i].getName() == Name) return &Values[i];
638 void addTemplateArg(const std::string &Name) {
639 assert(!isTemplateArg(Name) && "Template arg already defined!");
640 TemplateArgs.push_back(Name);
643 void addValue(const RecordVal &RV) {
644 assert(getValue(RV.getName()) == 0 && "Value already added!");
645 Values.push_back(RV);
648 void removeValue(const std::string &Name) {
649 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
650 for (unsigned i = 0, e = Values.size(); i != e; ++i)
651 if (Values[i].getName() == Name) {
652 Values.erase(Values.begin()+i);
655 assert(0 && "Name does not exist in record!");
658 bool isSubClassOf(Record *R) const {
659 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
660 if (SuperClasses[i] == R)
665 void addSuperClass(Record *R) {
666 assert(!isSubClassOf(R) && "Already subclassing record!");
667 SuperClasses.push_back(R);
670 // resolveReferences - If there are any field references that refer to fields
671 // that have been filled in, we can propagate the values now.
673 void resolveReferences();
677 //===--------------------------------------------------------------------===//
678 // High-level methods useful to tablegen back-ends
681 /// getValueInit - Return the initializer for a value with the specified name,
682 /// or throw an exception if the field does not exist.
684 Init *getValueInit(const std::string &FieldName) const;
686 /// getValueAsString - This method looks up the specified field and returns
687 /// its value as a string, throwing an exception if the field does not exist
688 /// or if the value is not a string.
690 std::string getValueAsString(const std::string &FieldName) const;
692 /// getValueAsBitsInit - This method looks up the specified field and returns
693 /// its value as a BitsInit, throwing an exception if the field does not exist
694 /// or if the value is not the right type.
696 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
698 /// getValueAsListInit - This method looks up the specified field and returns
699 /// its value as a ListInit, throwing an exception if the field does not exist
700 /// or if the value is not the right type.
702 ListInit *getValueAsListInit(const std::string &FieldName) const;
704 /// getValueAsDef - This method looks up the specified field and returns its
705 /// value as a Record, throwing an exception if the field does not exist or if
706 /// the value is not the right type.
708 Record *getValueAsDef(const std::string &FieldName) const;
710 /// getValueAsInt - This method looks up the specified field and returns its
711 /// value as an int, throwing an exception if the field does not exist or if
712 /// the value is not the right type.
714 int getValueAsInt(const std::string &FieldName) const;
717 std::ostream &operator<<(std::ostream &OS, const Record &R);
720 std::map<std::string, Record*> Classes, Defs;
723 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
724 E = Classes.end(); I != E; ++I)
726 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
727 E = Defs.end(); I != E; ++I)
731 const std::map<std::string, Record*> &getClasses() const { return Classes; }
732 const std::map<std::string, Record*> &getDefs() const { return Defs; }
734 Record *getClass(const std::string &Name) const {
735 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
736 return I == Classes.end() ? 0 : I->second;
738 Record *getDef(const std::string &Name) const {
739 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
740 return I == Defs.end() ? 0 : I->second;
742 void addClass(Record *R) {
743 assert(getClass(R->getName()) == 0 && "Class already exists!");
744 Classes.insert(std::make_pair(R->getName(), R));
746 void addDef(Record *R) {
747 assert(getDef(R->getName()) == 0 && "Def already exists!");
748 Defs.insert(std::make_pair(R->getName(), R));
751 //===--------------------------------------------------------------------===//
752 // High-level helper methods, useful for tablegen backends...
754 /// getAllDerivedDefinitions - This method returns all concrete definitions
755 /// that derive from the specified class name. If a class with the specified
756 /// name does not exist, an exception is thrown.
758 getAllDerivedDefinitions(const std::string &ClassName) const;
764 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
766 extern RecordKeeper Records;