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);
141 void print(std::ostream &OS) const;
146 //===----------------------------------------------------------------------===//
147 // Initializer Classes
148 //===----------------------------------------------------------------------===//
153 /// isComplete - This virtual method should be overridden by values that may
154 /// not be completely specified yet.
155 virtual bool isComplete() const { return true; }
157 /// print - Print out this value.
158 virtual void print(std::ostream &OS) const = 0;
160 /// dump - Debugging method that may be called through a debugger, just
161 /// invokes print on cerr.
164 /// convertInitializerTo - This virtual function is a simple call-back
165 /// function that should be overridden to call the appropriate
166 /// RecTy::convertValue method.
168 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
170 /// convertInitializerBitRange - This method is used to implement the bitrange
171 /// selection operator. Given an initializer, it selects the specified bits
172 /// out, returning them as a new init of bits type. If it is not legal to use
173 /// the bit subscript operator on this initializer, return null.
175 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
179 /// getFieldType - This method is used to implement the FieldInit class.
180 /// Implementors of this method should return the type of the named field if
181 /// they are of record type.
183 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
185 /// getFieldInit - This method complements getFieldType to return the
186 /// initializer for the specified field. If getFieldType returns non-null
187 /// this method should return non-null, otherwise it returns null.
189 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
193 /// resolveReferences - This method is used by classes that refer to other
194 /// variables which may not be defined at the time they expression is formed.
195 /// If a value is set for the variable later, this method will be called on
196 /// users of the value to allow the value to propagate out.
198 virtual Init *resolveReferences(Record &R) { return this; }
201 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
202 I.print(OS); return OS;
206 /// UnsetInit - ? - Represents an uninitialized value
208 struct UnsetInit : public Init {
209 virtual Init *convertInitializerTo(RecTy *Ty) {
210 return Ty->convertValue(this);
213 virtual bool isComplete() const { return false; }
214 virtual void print(std::ostream &OS) const { OS << "?"; }
218 /// BitInit - true/false - Represent a concrete initializer for a bit.
220 class BitInit : public Init {
223 BitInit(bool V) : Value(V) {}
225 bool getValue() const { return Value; }
227 virtual Init *convertInitializerTo(RecTy *Ty) {
228 return Ty->convertValue(this);
231 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
234 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
235 /// It contains a vector of bits, whose size is determined by the type.
237 class BitsInit : public Init {
238 std::vector<Init*> Bits;
240 BitsInit(unsigned Size) : Bits(Size) {}
242 unsigned getNumBits() const { return Bits.size(); }
244 Init *getBit(unsigned Bit) const {
245 assert(Bit < Bits.size() && "Bit index out of range!");
248 void setBit(unsigned Bit, Init *V) {
249 assert(Bit < Bits.size() && "Bit index out of range!");
250 assert(Bits[Bit] == 0 && "Bit already set!");
254 virtual Init *convertInitializerTo(RecTy *Ty) {
255 return Ty->convertValue(this);
257 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
259 virtual bool isComplete() const {
260 for (unsigned i = 0; i != getNumBits(); ++i)
261 if (!getBit(i)->isComplete()) return false;
264 virtual void print(std::ostream &OS) const;
266 virtual Init *resolveReferences(Record &R);
268 // printXX - Print this bitstream with the specified format, returning true if
269 // it is not possible.
270 bool printInHex(std::ostream &OS) const;
271 bool printAsVariable(std::ostream &OS) const;
272 bool printAsUnset(std::ostream &OS) const;
276 /// IntInit - 7 - Represent an initalization by a literal integer value.
278 class IntInit : public Init {
281 IntInit(int V) : Value(V) {}
283 int getValue() const { return Value; }
285 virtual Init *convertInitializerTo(RecTy *Ty) {
286 return Ty->convertValue(this);
288 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
290 virtual void print(std::ostream &OS) const { OS << Value; }
294 /// StringInit - "foo" - Represent an initialization by a string value.
296 class StringInit : public Init {
299 StringInit(const std::string &V) : Value(V) {}
301 virtual Init *convertInitializerTo(RecTy *Ty) {
302 return Ty->convertValue(this);
305 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
308 /// ListInit - [AL, AH, CL] - Represent a list of defs
310 class ListInit : public Init {
311 std::vector<Record*> Records;
313 ListInit(std::vector<Record*> &Rs) {
317 unsigned getSize() const { return Records.size(); }
318 Record *getElement(unsigned i) const {
319 assert(i < Records.size() && "List element index out of range!");
323 virtual Init *convertInitializerTo(RecTy *Ty) {
324 return Ty->convertValue(this);
327 virtual void print(std::ostream &OS) const;
331 /// TypedInit - This is the common super-class of types that have a specific,
334 class TypedInit : public Init {
337 TypedInit(RecTy *T) : Ty(T) {}
339 RecTy *getType() const { return Ty; }
341 /// resolveBitReference - This method is used to implement
342 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
343 /// simply return the resolved value, otherwise we return this.
345 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
348 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
350 class VarInit : public TypedInit {
353 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
355 virtual Init *convertInitializerTo(RecTy *Ty) {
356 return Ty->convertValue(this);
359 const std::string &getName() const { return VarName; }
361 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
363 virtual Init *resolveBitReference(Record &R, unsigned Bit);
365 virtual RecTy *getFieldType(const std::string &FieldName) const;
366 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
368 virtual void print(std::ostream &OS) const { OS << VarName; }
372 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
374 class VarBitInit : public Init {
378 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
379 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
380 ((BitsRecTy*)T->getType())->getNumBits() > B &&
381 "Illegal VarBitInit expression!");
384 virtual Init *convertInitializerTo(RecTy *Ty) {
385 return Ty->convertValue(this);
388 TypedInit *getVariable() const { return TI; }
389 unsigned getBitNum() const { return Bit; }
391 virtual void print(std::ostream &OS) const {
392 TI->print(OS); OS << "{" << Bit << "}";
394 virtual Init *resolveReferences(Record &R);
398 /// DefInit - AL - Represent a reference to a 'def' in the description
400 class DefInit : public Init {
403 DefInit(Record *D) : Def(D) {}
405 virtual Init *convertInitializerTo(RecTy *Ty) {
406 return Ty->convertValue(this);
409 Record *getDef() const { return Def; }
411 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
413 virtual RecTy *getFieldType(const std::string &FieldName) const;
414 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
416 virtual void print(std::ostream &OS) const;
420 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
422 class FieldInit : public TypedInit {
423 Init *Rec; // Record we are referring to
424 std::string FieldName; // Field we are accessing
426 FieldInit(Init *R, const std::string &FN)
427 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
428 assert(getType() && "FieldInit with non-record type!");
431 virtual Init *convertInitializerTo(RecTy *Ty) {
432 return Ty->convertValue(this);
435 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
437 virtual Init *resolveBitReference(Record &R, unsigned Bit);
439 virtual void print(std::ostream &OS) const {
440 Rec->print(OS); OS << "." << FieldName;
445 //===----------------------------------------------------------------------===//
446 // High-Level Classes
447 //===----------------------------------------------------------------------===//
455 RecordVal(const std::string &N, RecTy *T, unsigned P);
457 const std::string &getName() const { return Name; }
459 unsigned getPrefix() const { return Prefix; }
460 RecTy *getType() const { return Ty; }
461 Init *getValue() const { return Value; }
463 bool setValue(Init *V) {
465 Value = V->convertInitializerTo(Ty);
473 void print(std::ostream &OS, bool PrintSem = true) const;
476 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
482 const std::string Name;
483 std::vector<std::string> TemplateArgs;
484 std::vector<RecordVal> Values;
485 std::vector<Record*> SuperClasses;
488 Record(const std::string &N) : Name(N) {}
491 const std::string &getName() const { return Name; }
492 const std::vector<std::string> &getTemplateArgs() const {
495 const std::vector<RecordVal> &getValues() const { return Values; }
496 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
498 bool isTemplateArg(const std::string &Name) const {
499 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
500 if (TemplateArgs[i] == Name) return true;
504 const RecordVal *getValue(const std::string &Name) const {
505 for (unsigned i = 0, e = Values.size(); i != e; ++i)
506 if (Values[i].getName() == Name) return &Values[i];
509 RecordVal *getValue(const std::string &Name) {
510 for (unsigned i = 0, e = Values.size(); i != e; ++i)
511 if (Values[i].getName() == Name) return &Values[i];
515 void addTemplateArg(const std::string &Name) {
516 assert(!isTemplateArg(Name) && "Template arg already defined!");
517 TemplateArgs.push_back(Name);
520 void addValue(const RecordVal &RV) {
521 assert(getValue(RV.getName()) == 0 && "Value already added!");
522 Values.push_back(RV);
525 bool isSubClassOf(Record *R) const {
526 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
527 if (SuperClasses[i] == R)
532 void addSuperClass(Record *R) {
533 assert(!isSubClassOf(R) && "Already subclassing record!");
534 SuperClasses.push_back(R);
537 // resolveReferences - If there are any field references that refer to fields
538 // that have been filled in, we can propagate the values now.
540 void resolveReferences();
545 std::ostream &operator<<(std::ostream &OS, const Record &R);
548 std::map<std::string, Record*> Classes, Defs;
551 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
552 E = Classes.end(); I != E; ++I)
554 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
555 E = Defs.end(); I != E; ++I)
559 const std::map<std::string, Record*> &getClasses() const { return Classes; }
560 const std::map<std::string, Record*> &getDefs() const { return Defs; }
562 Record *getClass(const std::string &Name) const {
563 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
564 return I == Classes.end() ? 0 : I->second;
566 Record *getDef(const std::string &Name) const {
567 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
568 return I == Defs.end() ? 0 : I->second;
570 void addClass(Record *R) {
571 assert(getClass(R->getName()) == 0 && "Class already exists!");
572 Classes.insert(std::make_pair(R->getName(), R));
574 void addDef(Record *R) {
575 assert(getDef(R->getName()) == 0 && "Def already exists!");
576 Defs.insert(std::make_pair(R->getName(), R));
582 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
584 extern RecordKeeper Records;