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 *VI);
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) {}
122 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
123 Init *convertValue(ListInit *LI);
125 void print(std::ostream &OS) const;
128 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
131 class RecordRecTy : public RecTy {
134 RecordRecTy(Record *R) : Rec(R) {}
136 Record *getRecord() const { return Rec; }
138 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
139 Init *convertValue( DefInit *DI);
140 Init *convertValue(TypedInit *VI);
142 void print(std::ostream &OS) const;
147 //===----------------------------------------------------------------------===//
148 // Initializer Classes
149 //===----------------------------------------------------------------------===//
154 /// isComplete - This virtual method should be overridden by values that may
155 /// not be completely specified yet.
156 virtual bool isComplete() const { return true; }
158 /// print - Print out this value.
159 virtual void print(std::ostream &OS) const = 0;
161 /// dump - Debugging method that may be called through a debugger, just
162 /// invokes print on cerr.
165 /// convertInitializerTo - This virtual function is a simple call-back
166 /// function that should be overridden to call the appropriate
167 /// RecTy::convertValue method.
169 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
171 /// convertInitializerBitRange - This method is used to implement the bitrange
172 /// selection operator. Given an initializer, it selects the specified bits
173 /// out, returning them as a new init of bits type. If it is not legal to use
174 /// the bit subscript operator on this initializer, return null.
176 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
180 /// getFieldType - This method is used to implement the FieldInit class.
181 /// Implementors of this method should return the type of the named field if
182 /// they are of record type.
184 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
186 /// getFieldInit - This method complements getFieldType to return the
187 /// initializer for the specified field. If getFieldType returns non-null
188 /// this method should return non-null, otherwise it returns null.
190 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
194 /// resolveReferences - This method is used by classes that refer to other
195 /// variables which may not be defined at the time they expression is formed.
196 /// If a value is set for the variable later, this method will be called on
197 /// users of the value to allow the value to propagate out.
199 virtual Init *resolveReferences(Record &R) { return this; }
202 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
203 I.print(OS); return OS;
207 /// UnsetInit - ? - Represents an uninitialized value
209 struct UnsetInit : public Init {
210 virtual Init *convertInitializerTo(RecTy *Ty) {
211 return Ty->convertValue(this);
214 virtual bool isComplete() const { return false; }
215 virtual void print(std::ostream &OS) const { OS << "?"; }
219 /// BitInit - true/false - Represent a concrete initializer for a bit.
221 class BitInit : public Init {
224 BitInit(bool V) : Value(V) {}
226 bool getValue() const { return Value; }
228 virtual Init *convertInitializerTo(RecTy *Ty) {
229 return Ty->convertValue(this);
232 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
235 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
236 /// It contains a vector of bits, whose size is determined by the type.
238 class BitsInit : public Init {
239 std::vector<Init*> Bits;
241 BitsInit(unsigned Size) : Bits(Size) {}
243 unsigned getNumBits() const { return Bits.size(); }
245 Init *getBit(unsigned Bit) const {
246 assert(Bit < Bits.size() && "Bit index out of range!");
249 void setBit(unsigned Bit, Init *V) {
250 assert(Bit < Bits.size() && "Bit index out of range!");
251 assert(Bits[Bit] == 0 && "Bit already set!");
255 virtual Init *convertInitializerTo(RecTy *Ty) {
256 return Ty->convertValue(this);
258 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
260 virtual bool isComplete() const {
261 for (unsigned i = 0; i != getNumBits(); ++i)
262 if (!getBit(i)->isComplete()) return false;
265 virtual void print(std::ostream &OS) const;
267 virtual Init *resolveReferences(Record &R);
269 // printXX - Print this bitstream with the specified format, returning true if
270 // it is not possible.
271 bool printInHex(std::ostream &OS) const;
272 bool printAsVariable(std::ostream &OS) const;
273 bool printAsUnset(std::ostream &OS) const;
277 /// IntInit - 7 - Represent an initalization by a literal integer value.
279 class IntInit : public Init {
282 IntInit(int V) : Value(V) {}
284 int getValue() const { return Value; }
286 virtual Init *convertInitializerTo(RecTy *Ty) {
287 return Ty->convertValue(this);
289 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
291 virtual void print(std::ostream &OS) const { OS << Value; }
295 /// StringInit - "foo" - Represent an initialization by a string value.
297 class StringInit : public Init {
300 StringInit(const std::string &V) : Value(V) {}
302 virtual Init *convertInitializerTo(RecTy *Ty) {
303 return Ty->convertValue(this);
306 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
309 /// ListInit - [AL, AH, CL] - Represent a list of defs
311 class ListInit : public Init {
312 std::vector<Record*> Records;
314 ListInit(std::vector<Record*> &Rs) {
318 unsigned getSize() const { return Records.size(); }
319 Record *getElement(unsigned i) const {
320 assert(i < Records.size() && "List element index out of range!");
324 virtual Init *convertInitializerTo(RecTy *Ty) {
325 return Ty->convertValue(this);
328 virtual void print(std::ostream &OS) const;
332 /// TypedInit - This is the common super-class of types that have a specific,
335 class TypedInit : public Init {
338 TypedInit(RecTy *T) : Ty(T) {}
340 RecTy *getType() const { return Ty; }
342 /// resolveBitReference - This method is used to implement
343 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
344 /// simply return the resolved value, otherwise we return this.
346 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
349 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
351 class VarInit : public TypedInit {
354 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
356 virtual Init *convertInitializerTo(RecTy *Ty) {
357 return Ty->convertValue(this);
360 const std::string &getName() const { return VarName; }
362 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
364 virtual Init *resolveBitReference(Record &R, unsigned Bit);
366 virtual RecTy *getFieldType(const std::string &FieldName) const;
367 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
369 virtual void print(std::ostream &OS) const { OS << VarName; }
373 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
375 class VarBitInit : public Init {
379 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
380 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
381 ((BitsRecTy*)T->getType())->getNumBits() > B &&
382 "Illegal VarBitInit expression!");
385 virtual Init *convertInitializerTo(RecTy *Ty) {
386 return Ty->convertValue(this);
389 TypedInit *getVariable() const { return TI; }
390 unsigned getBitNum() const { return Bit; }
392 virtual void print(std::ostream &OS) const {
393 TI->print(OS); OS << "{" << Bit << "}";
395 virtual Init *resolveReferences(Record &R);
399 /// DefInit - AL - Represent a reference to a 'def' in the description
401 class DefInit : public Init {
404 DefInit(Record *D) : Def(D) {}
406 virtual Init *convertInitializerTo(RecTy *Ty) {
407 return Ty->convertValue(this);
410 Record *getDef() const { return Def; }
412 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
414 virtual RecTy *getFieldType(const std::string &FieldName) const;
415 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
417 virtual void print(std::ostream &OS) const;
421 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
423 class FieldInit : public TypedInit {
424 Init *Rec; // Record we are referring to
425 std::string FieldName; // Field we are accessing
427 FieldInit(Init *R, const std::string &FN)
428 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
429 assert(getType() && "FieldInit with non-record type!");
432 virtual Init *convertInitializerTo(RecTy *Ty) {
433 return Ty->convertValue(this);
436 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
438 virtual Init *resolveBitReference(Record &R, unsigned Bit);
440 virtual void print(std::ostream &OS) const {
441 Rec->print(OS); OS << "." << FieldName;
446 //===----------------------------------------------------------------------===//
447 // High-Level Classes
448 //===----------------------------------------------------------------------===//
456 RecordVal(const std::string &N, RecTy *T, unsigned P);
458 const std::string &getName() const { return Name; }
460 unsigned getPrefix() const { return Prefix; }
461 RecTy *getType() const { return Ty; }
462 Init *getValue() const { return Value; }
464 bool setValue(Init *V) {
466 Value = V->convertInitializerTo(Ty);
474 void print(std::ostream &OS, bool PrintSem = true) const;
477 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
483 const std::string Name;
484 std::vector<std::string> TemplateArgs;
485 std::vector<RecordVal> Values;
486 std::vector<Record*> SuperClasses;
489 Record(const std::string &N) : Name(N) {}
492 const std::string &getName() const { return Name; }
493 const std::vector<std::string> &getTemplateArgs() const {
496 const std::vector<RecordVal> &getValues() const { return Values; }
497 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
499 bool isTemplateArg(const std::string &Name) const {
500 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
501 if (TemplateArgs[i] == Name) return true;
505 const RecordVal *getValue(const std::string &Name) const {
506 for (unsigned i = 0, e = Values.size(); i != e; ++i)
507 if (Values[i].getName() == Name) return &Values[i];
510 RecordVal *getValue(const std::string &Name) {
511 for (unsigned i = 0, e = Values.size(); i != e; ++i)
512 if (Values[i].getName() == Name) return &Values[i];
516 void addTemplateArg(const std::string &Name) {
517 assert(!isTemplateArg(Name) && "Template arg already defined!");
518 TemplateArgs.push_back(Name);
521 void addValue(const RecordVal &RV) {
522 assert(getValue(RV.getName()) == 0 && "Value already added!");
523 Values.push_back(RV);
526 bool isSubClassOf(Record *R) const {
527 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
528 if (SuperClasses[i] == R)
533 void addSuperClass(Record *R) {
534 assert(!isSubClassOf(R) && "Already subclassing record!");
535 SuperClasses.push_back(R);
538 // resolveReferences - If there are any field references that refer to fields
539 // that have been filled in, we can propagate the values now.
541 void resolveReferences();
546 std::ostream &operator<<(std::ostream &OS, const Record &R);
549 std::map<std::string, Record*> Classes, Defs;
552 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
553 E = Classes.end(); I != E; ++I)
555 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
556 E = Defs.end(); I != E; ++I)
560 const std::map<std::string, Record*> &getClasses() const { return Classes; }
561 const std::map<std::string, Record*> &getDefs() const { return Defs; }
563 Record *getClass(const std::string &Name) const {
564 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
565 return I == Classes.end() ? 0 : I->second;
567 Record *getDef(const std::string &Name) const {
568 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
569 return I == Defs.end() ? 0 : I->second;
571 void addClass(Record *R) {
572 assert(getClass(R->getName()) == 0 && "Class already exists!");
573 Classes.insert(std::make_pair(R->getName(), R));
575 void addDef(Record *R) {
576 assert(getDef(R->getName()) == 0 && "Def already exists!");
577 Defs.insert(std::make_pair(R->getName(), R));
583 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
585 extern RecordKeeper Records;