1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
4 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 //===----------------------------------------------------------------------===//
34 virtual Init *convertValue( UnsetInit *UI) { return 0; }
35 virtual Init *convertValue( BitInit *BI) { return 0; }
36 virtual Init *convertValue( BitsInit *BI) { return 0; }
37 virtual Init *convertValue( IntInit *II) { return 0; }
38 virtual Init *convertValue(StringInit *SI) { return 0; }
39 virtual Init *convertValue( ListInit *LI) { return 0; }
40 virtual Init *convertValue(VarBitInit *VB) { return 0; }
41 virtual Init *convertValue( DefInit *DI) { return 0; }
42 virtual Init *convertValue( TypedInit *TI) { return 0; }
43 virtual Init *convertValue( VarInit *VI) {
44 return convertValue((TypedInit*)VI);
46 virtual Init *convertValue( FieldInit *FI) {
47 return convertValue((TypedInit*)FI);
50 virtual void print(std::ostream &OS) const = 0;
54 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
60 /// BitRecTy - 'bit' - Represent a single bit
62 struct BitRecTy : public RecTy {
63 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
64 Init *convertValue(BitInit *BI) { return (Init*)BI; }
65 Init *convertValue(BitsInit *BI);
66 Init *convertValue(IntInit *II);
67 Init *convertValue(TypedInit *VI);
68 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
70 void print(std::ostream &OS) const { OS << "bit"; }
74 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
76 class BitsRecTy : public RecTy {
79 BitsRecTy(unsigned Sz) : Size(Sz) {}
81 unsigned getNumBits() const { return Size; }
83 Init *convertValue(UnsetInit *UI);
84 Init *convertValue(BitInit *UI);
85 Init *convertValue(BitsInit *BI);
86 Init *convertValue(IntInit *II);
87 Init *convertValue(TypedInit *VI);
89 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
93 /// IntRecTy - 'int' - Represent an integer value of no particular size
95 struct IntRecTy : public RecTy {
96 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
97 Init *convertValue(IntInit *II) { return (Init*)II; }
98 Init *convertValue(BitsInit *BI);
99 Init *convertValue(TypedInit *TI);
101 void print(std::ostream &OS) const { OS << "int"; }
104 /// StringRecTy - 'string' - Represent an string value
106 struct StringRecTy : public RecTy {
107 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
108 Init *convertValue(StringInit *SI) { return (Init*)SI; }
109 Init *convertValue(TypedInit *VI);
110 void print(std::ostream &OS) const { OS << "string"; }
113 /// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
114 /// derived from the specified class.
116 class ListRecTy : public RecTy {
119 ListRecTy(Record *C) : Class(C) {}
120 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
121 Init *convertValue(ListInit *LI);
123 void print(std::ostream &OS) const;
126 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
129 class RecordRecTy : public RecTy {
132 RecordRecTy(Record *R) : Rec(R) {}
134 Record *getRecord() const { return Rec; }
136 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
137 Init *convertValue( DefInit *DI);
139 void print(std::ostream &OS) const;
144 //===----------------------------------------------------------------------===//
145 // Initializer Classes
146 //===----------------------------------------------------------------------===//
151 /// isComplete - This virtual method should be overridden by values that may
152 /// not be completely specified yet.
153 virtual bool isComplete() const { return true; }
155 /// print - Print out this value.
156 virtual void print(std::ostream &OS) const = 0;
158 /// dump - Debugging method that may be called through a debugger, just
159 /// invokes print on cerr.
162 /// convertInitializerTo - This virtual function is a simple call-back
163 /// function that should be overridden to call the appropriate
164 /// RecTy::convertValue method.
166 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
168 /// convertInitializerBitRange - This method is used to implement the bitrange
169 /// selection operator. Given an initializer, it selects the specified bits
170 /// out, returning them as a new init of bits type. If it is not legal to use
171 /// the bit subscript operator on this initializer, return null.
173 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
177 /// getFieldType - This method is used to implement the FieldInit class.
178 /// Implementors of this method should return the type of the named field if
179 /// they are of record type.
181 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
183 /// getFieldInit - This method complements getFieldType to return the
184 /// initializer for the specified field. If getFieldType returns non-null
185 /// this method should return non-null, otherwise it returns null.
187 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
191 /// resolveReferences - This method is used by classes that refer to other
192 /// variables which may not be defined at the time they expression is formed.
193 /// If a value is set for the variable later, this method will be called on
194 /// users of the value to allow the value to propagate out.
196 virtual Init *resolveReferences(Record &R) { return this; }
199 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
200 I.print(OS); return OS;
204 /// UnsetInit - ? - Represents an uninitialized value
206 struct UnsetInit : public Init {
207 virtual Init *convertInitializerTo(RecTy *Ty) {
208 return Ty->convertValue(this);
211 virtual bool isComplete() const { return false; }
212 virtual void print(std::ostream &OS) const { OS << "?"; }
216 /// BitInit - true/false - Represent a concrete initializer for a bit.
218 class BitInit : public Init {
221 BitInit(bool V) : Value(V) {}
223 bool getValue() const { return Value; }
225 virtual Init *convertInitializerTo(RecTy *Ty) {
226 return Ty->convertValue(this);
229 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
232 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
233 /// It contains a vector of bits, whose size is determined by the type.
235 class BitsInit : public Init {
236 std::vector<Init*> Bits;
238 BitsInit(unsigned Size) : Bits(Size) {}
240 unsigned getNumBits() const { return Bits.size(); }
242 Init *getBit(unsigned Bit) const {
243 assert(Bit < Bits.size() && "Bit index out of range!");
246 void setBit(unsigned Bit, Init *V) {
247 assert(Bit < Bits.size() && "Bit index out of range!");
248 assert(Bits[Bit] == 0 && "Bit already set!");
252 virtual Init *convertInitializerTo(RecTy *Ty) {
253 return Ty->convertValue(this);
255 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
257 virtual bool isComplete() const {
258 for (unsigned i = 0; i != getNumBits(); ++i)
259 if (!getBit(i)->isComplete()) return false;
262 virtual void print(std::ostream &OS) const;
264 virtual Init *resolveReferences(Record &R);
266 // printXX - Print this bitstream with the specified format, returning true if
267 // it is not possible.
268 bool printInHex(std::ostream &OS) const;
269 bool printAsVariable(std::ostream &OS) const;
270 bool printAsUnset(std::ostream &OS) const;
274 /// IntInit - 7 - Represent an initalization by a literal integer value.
276 class IntInit : public Init {
279 IntInit(int V) : Value(V) {}
281 int getValue() const { return Value; }
283 virtual Init *convertInitializerTo(RecTy *Ty) {
284 return Ty->convertValue(this);
286 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
288 virtual void print(std::ostream &OS) const { OS << Value; }
292 /// StringInit - "foo" - Represent an initialization by a string value.
294 class StringInit : public Init {
297 StringInit(const std::string &V) : Value(V) {}
299 virtual Init *convertInitializerTo(RecTy *Ty) {
300 return Ty->convertValue(this);
303 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
306 /// ListInit - [AL, AH, CL] - Represent a list of defs
308 class ListInit : public Init {
309 std::vector<Record*> Records;
311 ListInit(std::vector<Record*> &Rs) {
315 unsigned getSize() const { return Records.size(); }
316 Record *getElement(unsigned i) const {
317 assert(i < Records.size() && "List element index out of range!");
321 virtual Init *convertInitializerTo(RecTy *Ty) {
322 return Ty->convertValue(this);
325 virtual void print(std::ostream &OS) const;
329 /// TypedInit - This is the common super-class of types that have a specific,
332 class TypedInit : public Init {
335 TypedInit(RecTy *T) : Ty(T) {}
337 RecTy *getType() const { return Ty; }
339 /// resolveBitReference - This method is used to implement
340 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
341 /// simply return the resolved value, otherwise we return this.
343 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
346 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
348 class VarInit : public TypedInit {
351 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
353 virtual Init *convertInitializerTo(RecTy *Ty) {
354 return Ty->convertValue(this);
357 const std::string &getName() const { return VarName; }
359 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
361 virtual Init *resolveBitReference(Record &R, unsigned Bit);
363 virtual RecTy *getFieldType(const std::string &FieldName) const;
364 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
366 virtual void print(std::ostream &OS) const { OS << VarName; }
370 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
372 class VarBitInit : public Init {
376 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
377 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
378 ((BitsRecTy*)T->getType())->getNumBits() > B &&
379 "Illegal VarBitInit expression!");
382 virtual Init *convertInitializerTo(RecTy *Ty) {
383 return Ty->convertValue(this);
386 TypedInit *getVariable() const { return TI; }
387 unsigned getBitNum() const { return Bit; }
389 virtual void print(std::ostream &OS) const {
390 TI->print(OS); OS << "{" << Bit << "}";
392 virtual Init *resolveReferences(Record &R);
396 /// DefInit - AL - Represent a reference to a 'def' in the description
398 class DefInit : public Init {
401 DefInit(Record *D) : Def(D) {}
403 virtual Init *convertInitializerTo(RecTy *Ty) {
404 return Ty->convertValue(this);
407 Record *getDef() const { return Def; }
409 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
411 virtual RecTy *getFieldType(const std::string &FieldName) const;
412 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
414 virtual void print(std::ostream &OS) const;
418 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
420 class FieldInit : public TypedInit {
421 Init *Rec; // Record we are referring to
422 std::string FieldName; // Field we are accessing
424 FieldInit(Init *R, const std::string &FN)
425 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
426 assert(getType() && "FieldInit with non-record type!");
429 virtual Init *convertInitializerTo(RecTy *Ty) {
430 return Ty->convertValue(this);
433 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
435 virtual Init *resolveBitReference(Record &R, unsigned Bit);
437 virtual void print(std::ostream &OS) const {
438 Rec->print(OS); OS << "." << FieldName;
443 //===----------------------------------------------------------------------===//
444 // High-Level Classes
445 //===----------------------------------------------------------------------===//
453 RecordVal(const std::string &N, RecTy *T, unsigned P);
455 const std::string &getName() const { return Name; }
457 unsigned getPrefix() const { return Prefix; }
458 RecTy *getType() const { return Ty; }
459 Init *getValue() const { return Value; }
461 bool setValue(Init *V) {
463 Value = V->convertInitializerTo(Ty);
471 void print(std::ostream &OS, bool PrintSem = true) const;
474 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
480 const std::string Name;
481 std::vector<std::string> TemplateArgs;
482 std::vector<RecordVal> Values;
483 std::vector<Record*> SuperClasses;
486 Record(const std::string &N) : Name(N) {}
489 const std::string &getName() const { return Name; }
490 const std::vector<std::string> &getTemplateArgs() const {
493 const std::vector<RecordVal> &getValues() const { return Values; }
494 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
496 bool isTemplateArg(const std::string &Name) const {
497 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
498 if (TemplateArgs[i] == Name) return true;
502 const RecordVal *getValue(const std::string &Name) const {
503 for (unsigned i = 0, e = Values.size(); i != e; ++i)
504 if (Values[i].getName() == Name) return &Values[i];
507 RecordVal *getValue(const std::string &Name) {
508 for (unsigned i = 0, e = Values.size(); i != e; ++i)
509 if (Values[i].getName() == Name) return &Values[i];
513 void addTemplateArg(const std::string &Name) {
514 assert(!isTemplateArg(Name) && "Template arg already defined!");
515 TemplateArgs.push_back(Name);
518 void addValue(const RecordVal &RV) {
519 assert(getValue(RV.getName()) == 0 && "Value already added!");
520 Values.push_back(RV);
523 bool isSubClassOf(Record *R) const {
524 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
525 if (SuperClasses[i] == R)
530 void addSuperClass(Record *R) {
531 assert(!isSubClassOf(R) && "Already subclassing record!");
532 SuperClasses.push_back(R);
535 // resolveReferences - If there are any field references that refer to fields
536 // that have been filled in, we can propagate the values now.
538 void resolveReferences();
543 std::ostream &operator<<(std::ostream &OS, const Record &R);
546 std::map<std::string, Record*> Classes, Defs;
549 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
550 E = Classes.end(); I != E; ++I)
552 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
553 E = Defs.end(); I != E; ++I)
557 const std::map<std::string, Record*> &getClasses() const { return Classes; }
558 const std::map<std::string, Record*> &getDefs() const { return Defs; }
560 Record *getClass(const std::string &Name) const {
561 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
562 return I == Classes.end() ? 0 : I->second;
564 Record *getDef(const std::string &Name) const {
565 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
566 return I == Defs.end() ? 0 : I->second;
568 void addClass(Record *R) {
569 assert(getClass(R->getName()) == 0 && "Class already exists!");
570 Classes.insert(std::make_pair(R->getName(), R));
572 void addDef(Record *R) {
573 assert(getDef(R->getName()) == 0 && "Def already exists!");
574 Defs.insert(std::make_pair(R->getName(), R));
580 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
582 extern RecordKeeper Records;