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 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
39 virtual Init *convertValue( UnsetInit *UI) { return 0; }
40 virtual Init *convertValue( BitInit *BI) { return 0; }
41 virtual Init *convertValue( BitsInit *BI) { return 0; }
42 virtual Init *convertValue( IntInit *II) { return 0; }
43 virtual Init *convertValue(StringInit *SI) { return 0; }
44 virtual Init *convertValue( ListInit *LI) { return 0; }
45 virtual Init *convertValue( CodeInit *CI) { return 0; }
46 virtual Init *convertValue(VarBitInit *VB) { return 0; }
47 virtual Init *convertValue( DefInit *DI) { return 0; }
48 virtual Init *convertValue( TypedInit *TI) { return 0; }
49 virtual Init *convertValue( VarInit *VI) {
50 return convertValue((TypedInit*)VI);
52 virtual Init *convertValue( FieldInit *FI) {
53 return convertValue((TypedInit*)FI);
56 virtual void print(std::ostream &OS) const = 0;
60 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
66 /// BitRecTy - 'bit' - Represent a single bit
68 struct BitRecTy : public RecTy {
69 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
70 Init *convertValue(BitInit *BI) { return (Init*)BI; }
71 Init *convertValue(BitsInit *BI);
72 Init *convertValue(IntInit *II);
73 Init *convertValue(TypedInit *VI);
74 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
76 void print(std::ostream &OS) const { OS << "bit"; }
80 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
82 class BitsRecTy : public RecTy {
85 BitsRecTy(unsigned Sz) : Size(Sz) {}
87 unsigned getNumBits() const { return Size; }
89 Init *convertValue(UnsetInit *UI);
90 Init *convertValue(BitInit *UI);
91 Init *convertValue(BitsInit *BI);
92 Init *convertValue(IntInit *II);
93 Init *convertValue(TypedInit *VI);
95 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
99 /// IntRecTy - 'int' - Represent an integer value of no particular size
101 struct IntRecTy : public RecTy {
102 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
103 Init *convertValue(IntInit *II) { return (Init*)II; }
104 Init *convertValue(BitsInit *BI);
105 Init *convertValue(TypedInit *TI);
107 void print(std::ostream &OS) const { OS << "int"; }
110 /// StringRecTy - 'string' - Represent an string value
112 struct StringRecTy : public RecTy {
113 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
114 Init *convertValue(StringInit *SI) { return (Init*)SI; }
115 Init *convertValue(TypedInit *TI);
116 void print(std::ostream &OS) const { OS << "string"; }
119 /// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
120 /// derived from the specified class.
122 class ListRecTy : public RecTy {
125 ListRecTy(Record *C) : Class(C) {}
127 /// getElementClass - Return the class that the list contains.
129 Record *getElementClass() const { return Class; }
131 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
132 Init *convertValue(ListInit *LI);
133 Init *convertValue(TypedInit *TI);
135 void print(std::ostream &OS) const;
138 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
140 struct CodeRecTy : public RecTy {
141 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
142 Init *convertValue( CodeInit *CI) { return (Init*)CI; }
144 void print(std::ostream &OS) const { OS << "code"; }
148 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
151 class RecordRecTy : public RecTy {
154 RecordRecTy(Record *R) : Rec(R) {}
156 Record *getRecord() const { return Rec; }
158 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
159 Init *convertValue( DefInit *DI);
160 Init *convertValue(TypedInit *VI);
162 void print(std::ostream &OS) const;
167 //===----------------------------------------------------------------------===//
168 // Initializer Classes
169 //===----------------------------------------------------------------------===//
174 /// isComplete - This virtual method should be overridden by values that may
175 /// not be completely specified yet.
176 virtual bool isComplete() const { return true; }
178 /// print - Print out this value.
179 virtual void print(std::ostream &OS) const = 0;
181 /// dump - Debugging method that may be called through a debugger, just
182 /// invokes print on cerr.
185 /// convertInitializerTo - This virtual function is a simple call-back
186 /// function that should be overridden to call the appropriate
187 /// RecTy::convertValue method.
189 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
191 /// convertInitializerBitRange - This method is used to implement the bitrange
192 /// selection operator. Given an initializer, it selects the specified bits
193 /// out, returning them as a new init of bits type. If it is not legal to use
194 /// the bit subscript operator on this initializer, return null.
196 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
200 /// getFieldType - This method is used to implement the FieldInit class.
201 /// Implementors of this method should return the type of the named field if
202 /// they are of record type.
204 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
206 /// getFieldInit - This method complements getFieldType to return the
207 /// initializer for the specified field. If getFieldType returns non-null
208 /// this method should return non-null, otherwise it returns null.
210 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
214 /// resolveReferences - This method is used by classes that refer to other
215 /// variables which may not be defined at the time they expression is formed.
216 /// If a value is set for the variable later, this method will be called on
217 /// users of the value to allow the value to propagate out.
219 virtual Init *resolveReferences(Record &R) { return this; }
222 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
223 I.print(OS); return OS;
227 /// UnsetInit - ? - Represents an uninitialized value
229 struct UnsetInit : public Init {
230 virtual Init *convertInitializerTo(RecTy *Ty) {
231 return Ty->convertValue(this);
234 virtual bool isComplete() const { return false; }
235 virtual void print(std::ostream &OS) const { OS << "?"; }
239 /// BitInit - true/false - Represent a concrete initializer for a bit.
241 class BitInit : public Init {
244 BitInit(bool V) : Value(V) {}
246 bool getValue() const { return Value; }
248 virtual Init *convertInitializerTo(RecTy *Ty) {
249 return Ty->convertValue(this);
252 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
255 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
256 /// It contains a vector of bits, whose size is determined by the type.
258 class BitsInit : public Init {
259 std::vector<Init*> Bits;
261 BitsInit(unsigned Size) : Bits(Size) {}
263 unsigned getNumBits() const { return Bits.size(); }
265 Init *getBit(unsigned Bit) const {
266 assert(Bit < Bits.size() && "Bit index out of range!");
269 void setBit(unsigned Bit, Init *V) {
270 assert(Bit < Bits.size() && "Bit index out of range!");
271 assert(Bits[Bit] == 0 && "Bit already set!");
275 virtual Init *convertInitializerTo(RecTy *Ty) {
276 return Ty->convertValue(this);
278 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
280 virtual bool isComplete() const {
281 for (unsigned i = 0; i != getNumBits(); ++i)
282 if (!getBit(i)->isComplete()) return false;
285 virtual void print(std::ostream &OS) const;
287 virtual Init *resolveReferences(Record &R);
289 // printXX - Print this bitstream with the specified format, returning true if
290 // it is not possible.
291 bool printInHex(std::ostream &OS) const;
292 bool printAsVariable(std::ostream &OS) const;
293 bool printAsUnset(std::ostream &OS) const;
297 /// IntInit - 7 - Represent an initalization by a literal integer value.
299 class IntInit : public Init {
302 IntInit(int V) : Value(V) {}
304 int getValue() const { return Value; }
306 virtual Init *convertInitializerTo(RecTy *Ty) {
307 return Ty->convertValue(this);
309 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
311 virtual void print(std::ostream &OS) const { OS << Value; }
315 /// StringInit - "foo" - Represent an initialization by a string value.
317 class StringInit : public Init {
320 StringInit(const std::string &V) : Value(V) {}
322 virtual Init *convertInitializerTo(RecTy *Ty) {
323 return Ty->convertValue(this);
326 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
329 /// CodeInit - "[{...}]" - Represent a code fragment.
331 class CodeInit : public Init {
334 CodeInit(const std::string &V) : Value(V) {}
336 virtual Init *convertInitializerTo(RecTy *Ty) {
337 return Ty->convertValue(this);
340 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
343 /// ListInit - [AL, AH, CL] - Represent a list of defs
345 class ListInit : public Init {
346 std::vector<Record*> Records;
348 ListInit(std::vector<Record*> &Rs) {
352 unsigned getSize() const { return Records.size(); }
353 Record *getElement(unsigned i) const {
354 assert(i < Records.size() && "List element index out of range!");
358 virtual Init *convertInitializerTo(RecTy *Ty) {
359 return Ty->convertValue(this);
362 virtual void print(std::ostream &OS) const;
366 /// TypedInit - This is the common super-class of types that have a specific,
369 class TypedInit : public Init {
372 TypedInit(RecTy *T) : Ty(T) {}
374 RecTy *getType() const { return Ty; }
376 /// resolveBitReference - This method is used to implement
377 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
378 /// simply return the resolved value, otherwise we return this.
380 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
383 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
385 class VarInit : public TypedInit {
388 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
390 virtual Init *convertInitializerTo(RecTy *Ty) {
391 return Ty->convertValue(this);
394 const std::string &getName() const { return VarName; }
396 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
398 virtual Init *resolveBitReference(Record &R, unsigned Bit);
400 virtual RecTy *getFieldType(const std::string &FieldName) const;
401 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
403 /// resolveReferences - This method is used by classes that refer to other
404 /// variables which may not be defined at the time they expression is formed.
405 /// If a value is set for the variable later, this method will be called on
406 /// users of the value to allow the value to propagate out.
408 virtual Init *resolveReferences(Record &R);
410 virtual void print(std::ostream &OS) const { OS << VarName; }
414 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
416 class VarBitInit : public Init {
420 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
421 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
422 ((BitsRecTy*)T->getType())->getNumBits() > B &&
423 "Illegal VarBitInit expression!");
426 virtual Init *convertInitializerTo(RecTy *Ty) {
427 return Ty->convertValue(this);
430 TypedInit *getVariable() const { return TI; }
431 unsigned getBitNum() const { return Bit; }
433 virtual void print(std::ostream &OS) const {
434 TI->print(OS); OS << "{" << Bit << "}";
436 virtual Init *resolveReferences(Record &R);
440 /// DefInit - AL - Represent a reference to a 'def' in the description
442 class DefInit : public Init {
445 DefInit(Record *D) : Def(D) {}
447 virtual Init *convertInitializerTo(RecTy *Ty) {
448 return Ty->convertValue(this);
451 Record *getDef() const { return Def; }
453 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
455 virtual RecTy *getFieldType(const std::string &FieldName) const;
456 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
458 virtual void print(std::ostream &OS) const;
462 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
464 class FieldInit : public TypedInit {
465 Init *Rec; // Record we are referring to
466 std::string FieldName; // Field we are accessing
468 FieldInit(Init *R, const std::string &FN)
469 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
470 assert(getType() && "FieldInit with non-record type!");
473 virtual Init *convertInitializerTo(RecTy *Ty) {
474 return Ty->convertValue(this);
477 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
479 virtual Init *resolveBitReference(Record &R, unsigned Bit);
481 virtual void print(std::ostream &OS) const {
482 Rec->print(OS); OS << "." << FieldName;
487 //===----------------------------------------------------------------------===//
488 // High-Level Classes
489 //===----------------------------------------------------------------------===//
497 RecordVal(const std::string &N, RecTy *T, unsigned P);
499 const std::string &getName() const { return Name; }
501 unsigned getPrefix() const { return Prefix; }
502 RecTy *getType() const { return Ty; }
503 Init *getValue() const { return Value; }
505 bool setValue(Init *V) {
507 Value = V->convertInitializerTo(Ty);
515 void print(std::ostream &OS, bool PrintSem = true) const;
518 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
524 const std::string Name;
525 std::vector<std::string> TemplateArgs;
526 std::vector<RecordVal> Values;
527 std::vector<Record*> SuperClasses;
530 Record(const std::string &N) : Name(N) {}
533 const std::string &getName() const { return Name; }
534 const std::vector<std::string> &getTemplateArgs() const {
537 const std::vector<RecordVal> &getValues() const { return Values; }
538 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
540 bool isTemplateArg(const std::string &Name) const {
541 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
542 if (TemplateArgs[i] == Name) return true;
546 const RecordVal *getValue(const std::string &Name) const {
547 for (unsigned i = 0, e = Values.size(); i != e; ++i)
548 if (Values[i].getName() == Name) return &Values[i];
551 RecordVal *getValue(const std::string &Name) {
552 for (unsigned i = 0, e = Values.size(); i != e; ++i)
553 if (Values[i].getName() == Name) return &Values[i];
557 void addTemplateArg(const std::string &Name) {
558 assert(!isTemplateArg(Name) && "Template arg already defined!");
559 TemplateArgs.push_back(Name);
562 void addValue(const RecordVal &RV) {
563 assert(getValue(RV.getName()) == 0 && "Value already added!");
564 Values.push_back(RV);
567 void removeValue(const std::string &Name) {
568 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
569 for (unsigned i = 0, e = Values.size(); i != e; ++i)
570 if (Values[i].getName() == Name) {
571 Values.erase(Values.begin()+i);
574 assert(0 && "Name does not exist in record!");
577 bool isSubClassOf(Record *R) const {
578 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
579 if (SuperClasses[i] == R)
584 void addSuperClass(Record *R) {
585 assert(!isSubClassOf(R) && "Already subclassing record!");
586 SuperClasses.push_back(R);
589 // resolveReferences - If there are any field references that refer to fields
590 // that have been filled in, we can propagate the values now.
592 void resolveReferences();
597 std::ostream &operator<<(std::ostream &OS, const Record &R);
600 std::map<std::string, Record*> Classes, Defs;
603 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
604 E = Classes.end(); I != E; ++I)
606 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
607 E = Defs.end(); I != E; ++I)
611 const std::map<std::string, Record*> &getClasses() const { return Classes; }
612 const std::map<std::string, Record*> &getDefs() const { return Defs; }
614 Record *getClass(const std::string &Name) const {
615 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
616 return I == Classes.end() ? 0 : I->second;
618 Record *getDef(const std::string &Name) const {
619 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
620 return I == Defs.end() ? 0 : I->second;
622 void addClass(Record *R) {
623 assert(getClass(R->getName()) == 0 && "Class already exists!");
624 Classes.insert(std::make_pair(R->getName(), R));
626 void addDef(Record *R) {
627 assert(getDef(R->getName()) == 0 && "Def already exists!");
628 Defs.insert(std::make_pair(R->getName(), R));
631 //===--------------------------------------------------------------------===//
632 // High-level helper methods, useful for tablegen backends...
634 /// getAllDerivedDefinitions - This method returns all concrete definitions
635 /// that derive from the specified class name. If a class with the specified
636 /// name does not exist, an error is printed and true is returned.
637 bool getAllDerivedDefinitions(const std::string &ClassName,
638 std::vector<Record*> &ReturnDefs) const;
644 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
646 extern RecordKeeper Records;