1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
4 //===----------------------------------------------------------------------===//
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
36 virtual Init *convertValue( UnsetInit *UI) { return 0; }
37 virtual Init *convertValue( BitInit *BI) { return 0; }
38 virtual Init *convertValue( BitsInit *BI) { return 0; }
39 virtual Init *convertValue( IntInit *II) { return 0; }
40 virtual Init *convertValue(StringInit *SI) { return 0; }
41 virtual Init *convertValue( ListInit *LI) { return 0; }
42 virtual Init *convertValue(VarBitInit *VB) { return 0; }
43 virtual Init *convertValue( DefInit *DI) { return 0; }
44 virtual Init *convertValue( TypedInit *TI) { return 0; }
45 virtual Init *convertValue( VarInit *VI) {
46 return convertValue((TypedInit*)VI);
48 virtual Init *convertValue( FieldInit *FI) {
49 return convertValue((TypedInit*)FI);
52 virtual void print(std::ostream &OS) const = 0;
56 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
62 /// BitRecTy - 'bit' - Represent a single bit
64 struct BitRecTy : public RecTy {
65 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
66 Init *convertValue(BitInit *BI) { return (Init*)BI; }
67 Init *convertValue(BitsInit *BI);
68 Init *convertValue(IntInit *II);
69 Init *convertValue(TypedInit *VI);
70 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
72 void print(std::ostream &OS) const { OS << "bit"; }
76 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
78 class BitsRecTy : public RecTy {
81 BitsRecTy(unsigned Sz) : Size(Sz) {}
83 unsigned getNumBits() const { return Size; }
85 Init *convertValue(UnsetInit *UI);
86 Init *convertValue(BitInit *UI);
87 Init *convertValue(BitsInit *BI);
88 Init *convertValue(IntInit *II);
89 Init *convertValue(TypedInit *VI);
91 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
95 /// IntRecTy - 'int' - Represent an integer value of no particular size
97 struct IntRecTy : public RecTy {
98 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
99 Init *convertValue(IntInit *II) { return (Init*)II; }
100 Init *convertValue(BitsInit *BI);
101 Init *convertValue(TypedInit *TI);
103 void print(std::ostream &OS) const { OS << "int"; }
106 /// StringRecTy - 'string' - Represent an string value
108 struct StringRecTy : public RecTy {
109 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
110 Init *convertValue(StringInit *SI) { return (Init*)SI; }
111 Init *convertValue(TypedInit *TI);
112 void print(std::ostream &OS) const { OS << "string"; }
115 /// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
116 /// derived from the specified class.
118 class ListRecTy : public RecTy {
121 ListRecTy(Record *C) : Class(C) {}
123 /// getElementClass - Return the class that the list contains.
125 Record *getElementClass() const { return Class; }
127 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
128 Init *convertValue(ListInit *LI);
129 Init *convertValue(TypedInit *TI);
131 void print(std::ostream &OS) const;
134 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
136 struct CodeRecTy : public RecTy {
137 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
139 void print(std::ostream &OS) const { OS << "code"; }
143 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
146 class RecordRecTy : public RecTy {
149 RecordRecTy(Record *R) : Rec(R) {}
151 Record *getRecord() const { return Rec; }
153 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
154 Init *convertValue( DefInit *DI);
155 Init *convertValue(TypedInit *VI);
157 void print(std::ostream &OS) const;
162 //===----------------------------------------------------------------------===//
163 // Initializer Classes
164 //===----------------------------------------------------------------------===//
169 /// isComplete - This virtual method should be overridden by values that may
170 /// not be completely specified yet.
171 virtual bool isComplete() const { return true; }
173 /// print - Print out this value.
174 virtual void print(std::ostream &OS) const = 0;
176 /// dump - Debugging method that may be called through a debugger, just
177 /// invokes print on cerr.
180 /// convertInitializerTo - This virtual function is a simple call-back
181 /// function that should be overridden to call the appropriate
182 /// RecTy::convertValue method.
184 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
186 /// convertInitializerBitRange - This method is used to implement the bitrange
187 /// selection operator. Given an initializer, it selects the specified bits
188 /// out, returning them as a new init of bits type. If it is not legal to use
189 /// the bit subscript operator on this initializer, return null.
191 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
195 /// getFieldType - This method is used to implement the FieldInit class.
196 /// Implementors of this method should return the type of the named field if
197 /// they are of record type.
199 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
201 /// getFieldInit - This method complements getFieldType to return the
202 /// initializer for the specified field. If getFieldType returns non-null
203 /// this method should return non-null, otherwise it returns null.
205 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
209 /// resolveReferences - This method is used by classes that refer to other
210 /// variables which may not be defined at the time they expression is formed.
211 /// If a value is set for the variable later, this method will be called on
212 /// users of the value to allow the value to propagate out.
214 virtual Init *resolveReferences(Record &R) { return this; }
217 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
218 I.print(OS); return OS;
222 /// UnsetInit - ? - Represents an uninitialized value
224 struct UnsetInit : public Init {
225 virtual Init *convertInitializerTo(RecTy *Ty) {
226 return Ty->convertValue(this);
229 virtual bool isComplete() const { return false; }
230 virtual void print(std::ostream &OS) const { OS << "?"; }
234 /// BitInit - true/false - Represent a concrete initializer for a bit.
236 class BitInit : public Init {
239 BitInit(bool V) : Value(V) {}
241 bool getValue() const { return Value; }
243 virtual Init *convertInitializerTo(RecTy *Ty) {
244 return Ty->convertValue(this);
247 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
250 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
251 /// It contains a vector of bits, whose size is determined by the type.
253 class BitsInit : public Init {
254 std::vector<Init*> Bits;
256 BitsInit(unsigned Size) : Bits(Size) {}
258 unsigned getNumBits() const { return Bits.size(); }
260 Init *getBit(unsigned Bit) const {
261 assert(Bit < Bits.size() && "Bit index out of range!");
264 void setBit(unsigned Bit, Init *V) {
265 assert(Bit < Bits.size() && "Bit index out of range!");
266 assert(Bits[Bit] == 0 && "Bit already set!");
270 virtual Init *convertInitializerTo(RecTy *Ty) {
271 return Ty->convertValue(this);
273 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
275 virtual bool isComplete() const {
276 for (unsigned i = 0; i != getNumBits(); ++i)
277 if (!getBit(i)->isComplete()) return false;
280 virtual void print(std::ostream &OS) const;
282 virtual Init *resolveReferences(Record &R);
284 // printXX - Print this bitstream with the specified format, returning true if
285 // it is not possible.
286 bool printInHex(std::ostream &OS) const;
287 bool printAsVariable(std::ostream &OS) const;
288 bool printAsUnset(std::ostream &OS) const;
292 /// IntInit - 7 - Represent an initalization by a literal integer value.
294 class IntInit : public Init {
297 IntInit(int V) : Value(V) {}
299 int getValue() const { return Value; }
301 virtual Init *convertInitializerTo(RecTy *Ty) {
302 return Ty->convertValue(this);
304 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
306 virtual void print(std::ostream &OS) const { OS << Value; }
310 /// StringInit - "foo" - Represent an initialization by a string value.
312 class StringInit : public Init {
315 StringInit(const std::string &V) : Value(V) {}
317 virtual Init *convertInitializerTo(RecTy *Ty) {
318 return Ty->convertValue(this);
321 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
324 /// ListInit - [AL, AH, CL] - Represent a list of defs
326 class ListInit : public Init {
327 std::vector<Record*> Records;
329 ListInit(std::vector<Record*> &Rs) {
333 unsigned getSize() const { return Records.size(); }
334 Record *getElement(unsigned i) const {
335 assert(i < Records.size() && "List element index out of range!");
339 virtual Init *convertInitializerTo(RecTy *Ty) {
340 return Ty->convertValue(this);
343 virtual void print(std::ostream &OS) const;
347 /// TypedInit - This is the common super-class of types that have a specific,
350 class TypedInit : public Init {
353 TypedInit(RecTy *T) : Ty(T) {}
355 RecTy *getType() const { return Ty; }
357 /// resolveBitReference - This method is used to implement
358 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
359 /// simply return the resolved value, otherwise we return this.
361 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
364 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
366 class VarInit : public TypedInit {
369 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
371 virtual Init *convertInitializerTo(RecTy *Ty) {
372 return Ty->convertValue(this);
375 const std::string &getName() const { return VarName; }
377 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
379 virtual Init *resolveBitReference(Record &R, unsigned Bit);
381 virtual RecTy *getFieldType(const std::string &FieldName) const;
382 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
384 /// resolveReferences - This method is used by classes that refer to other
385 /// variables which may not be defined at the time they expression is formed.
386 /// If a value is set for the variable later, this method will be called on
387 /// users of the value to allow the value to propagate out.
389 virtual Init *resolveReferences(Record &R);
391 virtual void print(std::ostream &OS) const { OS << VarName; }
395 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
397 class VarBitInit : public Init {
401 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
402 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
403 ((BitsRecTy*)T->getType())->getNumBits() > B &&
404 "Illegal VarBitInit expression!");
407 virtual Init *convertInitializerTo(RecTy *Ty) {
408 return Ty->convertValue(this);
411 TypedInit *getVariable() const { return TI; }
412 unsigned getBitNum() const { return Bit; }
414 virtual void print(std::ostream &OS) const {
415 TI->print(OS); OS << "{" << Bit << "}";
417 virtual Init *resolveReferences(Record &R);
421 /// DefInit - AL - Represent a reference to a 'def' in the description
423 class DefInit : public Init {
426 DefInit(Record *D) : Def(D) {}
428 virtual Init *convertInitializerTo(RecTy *Ty) {
429 return Ty->convertValue(this);
432 Record *getDef() const { return Def; }
434 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
436 virtual RecTy *getFieldType(const std::string &FieldName) const;
437 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
439 virtual void print(std::ostream &OS) const;
443 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
445 class FieldInit : public TypedInit {
446 Init *Rec; // Record we are referring to
447 std::string FieldName; // Field we are accessing
449 FieldInit(Init *R, const std::string &FN)
450 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
451 assert(getType() && "FieldInit with non-record type!");
454 virtual Init *convertInitializerTo(RecTy *Ty) {
455 return Ty->convertValue(this);
458 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
460 virtual Init *resolveBitReference(Record &R, unsigned Bit);
462 virtual void print(std::ostream &OS) const {
463 Rec->print(OS); OS << "." << FieldName;
468 //===----------------------------------------------------------------------===//
469 // High-Level Classes
470 //===----------------------------------------------------------------------===//
478 RecordVal(const std::string &N, RecTy *T, unsigned P);
480 const std::string &getName() const { return Name; }
482 unsigned getPrefix() const { return Prefix; }
483 RecTy *getType() const { return Ty; }
484 Init *getValue() const { return Value; }
486 bool setValue(Init *V) {
488 Value = V->convertInitializerTo(Ty);
496 void print(std::ostream &OS, bool PrintSem = true) const;
499 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
505 const std::string Name;
506 std::vector<std::string> TemplateArgs;
507 std::vector<RecordVal> Values;
508 std::vector<Record*> SuperClasses;
511 Record(const std::string &N) : Name(N) {}
514 const std::string &getName() const { return Name; }
515 const std::vector<std::string> &getTemplateArgs() const {
518 const std::vector<RecordVal> &getValues() const { return Values; }
519 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
521 bool isTemplateArg(const std::string &Name) const {
522 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
523 if (TemplateArgs[i] == Name) return true;
527 const RecordVal *getValue(const std::string &Name) const {
528 for (unsigned i = 0, e = Values.size(); i != e; ++i)
529 if (Values[i].getName() == Name) return &Values[i];
532 RecordVal *getValue(const std::string &Name) {
533 for (unsigned i = 0, e = Values.size(); i != e; ++i)
534 if (Values[i].getName() == Name) return &Values[i];
538 void addTemplateArg(const std::string &Name) {
539 assert(!isTemplateArg(Name) && "Template arg already defined!");
540 TemplateArgs.push_back(Name);
543 void addValue(const RecordVal &RV) {
544 assert(getValue(RV.getName()) == 0 && "Value already added!");
545 Values.push_back(RV);
548 void removeValue(const std::string &Name) {
549 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
550 for (unsigned i = 0, e = Values.size(); i != e; ++i)
551 if (Values[i].getName() == Name) {
552 Values.erase(Values.begin()+i);
555 assert(0 && "Name does not exist in record!");
558 bool isSubClassOf(Record *R) const {
559 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
560 if (SuperClasses[i] == R)
565 void addSuperClass(Record *R) {
566 assert(!isSubClassOf(R) && "Already subclassing record!");
567 SuperClasses.push_back(R);
570 // resolveReferences - If there are any field references that refer to fields
571 // that have been filled in, we can propagate the values now.
573 void resolveReferences();
578 std::ostream &operator<<(std::ostream &OS, const Record &R);
581 std::map<std::string, Record*> Classes, Defs;
584 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
585 E = Classes.end(); I != E; ++I)
587 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
588 E = Defs.end(); I != E; ++I)
592 const std::map<std::string, Record*> &getClasses() const { return Classes; }
593 const std::map<std::string, Record*> &getDefs() const { return Defs; }
595 Record *getClass(const std::string &Name) const {
596 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
597 return I == Classes.end() ? 0 : I->second;
599 Record *getDef(const std::string &Name) const {
600 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
601 return I == Defs.end() ? 0 : I->second;
603 void addClass(Record *R) {
604 assert(getClass(R->getName()) == 0 && "Class already exists!");
605 Classes.insert(std::make_pair(R->getName(), R));
607 void addDef(Record *R) {
608 assert(getDef(R->getName()) == 0 && "Def already exists!");
609 Defs.insert(std::make_pair(R->getName(), R));
615 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
617 extern RecordKeeper Records;