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 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
137 class RecordRecTy : public RecTy {
140 RecordRecTy(Record *R) : Rec(R) {}
142 Record *getRecord() const { return Rec; }
144 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
145 Init *convertValue( DefInit *DI);
146 Init *convertValue(TypedInit *VI);
148 void print(std::ostream &OS) const;
153 //===----------------------------------------------------------------------===//
154 // Initializer Classes
155 //===----------------------------------------------------------------------===//
160 /// isComplete - This virtual method should be overridden by values that may
161 /// not be completely specified yet.
162 virtual bool isComplete() const { return true; }
164 /// print - Print out this value.
165 virtual void print(std::ostream &OS) const = 0;
167 /// dump - Debugging method that may be called through a debugger, just
168 /// invokes print on cerr.
171 /// convertInitializerTo - This virtual function is a simple call-back
172 /// function that should be overridden to call the appropriate
173 /// RecTy::convertValue method.
175 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
177 /// convertInitializerBitRange - This method is used to implement the bitrange
178 /// selection operator. Given an initializer, it selects the specified bits
179 /// out, returning them as a new init of bits type. If it is not legal to use
180 /// the bit subscript operator on this initializer, return null.
182 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
186 /// getFieldType - This method is used to implement the FieldInit class.
187 /// Implementors of this method should return the type of the named field if
188 /// they are of record type.
190 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
192 /// getFieldInit - This method complements getFieldType to return the
193 /// initializer for the specified field. If getFieldType returns non-null
194 /// this method should return non-null, otherwise it returns null.
196 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
200 /// resolveReferences - This method is used by classes that refer to other
201 /// variables which may not be defined at the time they expression is formed.
202 /// If a value is set for the variable later, this method will be called on
203 /// users of the value to allow the value to propagate out.
205 virtual Init *resolveReferences(Record &R) { return this; }
208 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
209 I.print(OS); return OS;
213 /// UnsetInit - ? - Represents an uninitialized value
215 struct UnsetInit : public Init {
216 virtual Init *convertInitializerTo(RecTy *Ty) {
217 return Ty->convertValue(this);
220 virtual bool isComplete() const { return false; }
221 virtual void print(std::ostream &OS) const { OS << "?"; }
225 /// BitInit - true/false - Represent a concrete initializer for a bit.
227 class BitInit : public Init {
230 BitInit(bool V) : Value(V) {}
232 bool getValue() const { return Value; }
234 virtual Init *convertInitializerTo(RecTy *Ty) {
235 return Ty->convertValue(this);
238 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
241 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
242 /// It contains a vector of bits, whose size is determined by the type.
244 class BitsInit : public Init {
245 std::vector<Init*> Bits;
247 BitsInit(unsigned Size) : Bits(Size) {}
249 unsigned getNumBits() const { return Bits.size(); }
251 Init *getBit(unsigned Bit) const {
252 assert(Bit < Bits.size() && "Bit index out of range!");
255 void setBit(unsigned Bit, Init *V) {
256 assert(Bit < Bits.size() && "Bit index out of range!");
257 assert(Bits[Bit] == 0 && "Bit already set!");
261 virtual Init *convertInitializerTo(RecTy *Ty) {
262 return Ty->convertValue(this);
264 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
266 virtual bool isComplete() const {
267 for (unsigned i = 0; i != getNumBits(); ++i)
268 if (!getBit(i)->isComplete()) return false;
271 virtual void print(std::ostream &OS) const;
273 virtual Init *resolveReferences(Record &R);
275 // printXX - Print this bitstream with the specified format, returning true if
276 // it is not possible.
277 bool printInHex(std::ostream &OS) const;
278 bool printAsVariable(std::ostream &OS) const;
279 bool printAsUnset(std::ostream &OS) const;
283 /// IntInit - 7 - Represent an initalization by a literal integer value.
285 class IntInit : public Init {
288 IntInit(int V) : Value(V) {}
290 int getValue() const { return Value; }
292 virtual Init *convertInitializerTo(RecTy *Ty) {
293 return Ty->convertValue(this);
295 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
297 virtual void print(std::ostream &OS) const { OS << Value; }
301 /// StringInit - "foo" - Represent an initialization by a string value.
303 class StringInit : public Init {
306 StringInit(const std::string &V) : Value(V) {}
308 virtual Init *convertInitializerTo(RecTy *Ty) {
309 return Ty->convertValue(this);
312 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
315 /// ListInit - [AL, AH, CL] - Represent a list of defs
317 class ListInit : public Init {
318 std::vector<Record*> Records;
320 ListInit(std::vector<Record*> &Rs) {
324 unsigned getSize() const { return Records.size(); }
325 Record *getElement(unsigned i) const {
326 assert(i < Records.size() && "List element index out of range!");
330 virtual Init *convertInitializerTo(RecTy *Ty) {
331 return Ty->convertValue(this);
334 virtual void print(std::ostream &OS) const;
338 /// TypedInit - This is the common super-class of types that have a specific,
341 class TypedInit : public Init {
344 TypedInit(RecTy *T) : Ty(T) {}
346 RecTy *getType() const { return Ty; }
348 /// resolveBitReference - This method is used to implement
349 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
350 /// simply return the resolved value, otherwise we return this.
352 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
355 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
357 class VarInit : public TypedInit {
360 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
362 virtual Init *convertInitializerTo(RecTy *Ty) {
363 return Ty->convertValue(this);
366 const std::string &getName() const { return VarName; }
368 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
370 virtual Init *resolveBitReference(Record &R, unsigned Bit);
372 virtual RecTy *getFieldType(const std::string &FieldName) const;
373 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
375 /// resolveReferences - This method is used by classes that refer to other
376 /// variables which may not be defined at the time they expression is formed.
377 /// If a value is set for the variable later, this method will be called on
378 /// users of the value to allow the value to propagate out.
380 virtual Init *resolveReferences(Record &R);
382 virtual void print(std::ostream &OS) const { OS << VarName; }
386 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
388 class VarBitInit : public Init {
392 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
393 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
394 ((BitsRecTy*)T->getType())->getNumBits() > B &&
395 "Illegal VarBitInit expression!");
398 virtual Init *convertInitializerTo(RecTy *Ty) {
399 return Ty->convertValue(this);
402 TypedInit *getVariable() const { return TI; }
403 unsigned getBitNum() const { return Bit; }
405 virtual void print(std::ostream &OS) const {
406 TI->print(OS); OS << "{" << Bit << "}";
408 virtual Init *resolveReferences(Record &R);
412 /// DefInit - AL - Represent a reference to a 'def' in the description
414 class DefInit : public Init {
417 DefInit(Record *D) : Def(D) {}
419 virtual Init *convertInitializerTo(RecTy *Ty) {
420 return Ty->convertValue(this);
423 Record *getDef() const { return Def; }
425 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
427 virtual RecTy *getFieldType(const std::string &FieldName) const;
428 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
430 virtual void print(std::ostream &OS) const;
434 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
436 class FieldInit : public TypedInit {
437 Init *Rec; // Record we are referring to
438 std::string FieldName; // Field we are accessing
440 FieldInit(Init *R, const std::string &FN)
441 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
442 assert(getType() && "FieldInit with non-record type!");
445 virtual Init *convertInitializerTo(RecTy *Ty) {
446 return Ty->convertValue(this);
449 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
451 virtual Init *resolveBitReference(Record &R, unsigned Bit);
453 virtual void print(std::ostream &OS) const {
454 Rec->print(OS); OS << "." << FieldName;
459 //===----------------------------------------------------------------------===//
460 // High-Level Classes
461 //===----------------------------------------------------------------------===//
469 RecordVal(const std::string &N, RecTy *T, unsigned P);
471 const std::string &getName() const { return Name; }
473 unsigned getPrefix() const { return Prefix; }
474 RecTy *getType() const { return Ty; }
475 Init *getValue() const { return Value; }
477 bool setValue(Init *V) {
479 Value = V->convertInitializerTo(Ty);
487 void print(std::ostream &OS, bool PrintSem = true) const;
490 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
496 const std::string Name;
497 std::vector<std::string> TemplateArgs;
498 std::vector<RecordVal> Values;
499 std::vector<Record*> SuperClasses;
502 Record(const std::string &N) : Name(N) {}
505 const std::string &getName() const { return Name; }
506 const std::vector<std::string> &getTemplateArgs() const {
509 const std::vector<RecordVal> &getValues() const { return Values; }
510 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
512 bool isTemplateArg(const std::string &Name) const {
513 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
514 if (TemplateArgs[i] == Name) return true;
518 const RecordVal *getValue(const std::string &Name) const {
519 for (unsigned i = 0, e = Values.size(); i != e; ++i)
520 if (Values[i].getName() == Name) return &Values[i];
523 RecordVal *getValue(const std::string &Name) {
524 for (unsigned i = 0, e = Values.size(); i != e; ++i)
525 if (Values[i].getName() == Name) return &Values[i];
529 void addTemplateArg(const std::string &Name) {
530 assert(!isTemplateArg(Name) && "Template arg already defined!");
531 TemplateArgs.push_back(Name);
534 void addValue(const RecordVal &RV) {
535 assert(getValue(RV.getName()) == 0 && "Value already added!");
536 Values.push_back(RV);
539 bool isSubClassOf(Record *R) const {
540 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
541 if (SuperClasses[i] == R)
546 void addSuperClass(Record *R) {
547 assert(!isSubClassOf(R) && "Already subclassing record!");
548 SuperClasses.push_back(R);
551 // resolveReferences - If there are any field references that refer to fields
552 // that have been filled in, we can propagate the values now.
554 void resolveReferences();
559 std::ostream &operator<<(std::ostream &OS, const Record &R);
562 std::map<std::string, Record*> Classes, Defs;
565 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
566 E = Classes.end(); I != E; ++I)
568 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
569 E = Defs.end(); I != E; ++I)
573 const std::map<std::string, Record*> &getClasses() const { return Classes; }
574 const std::map<std::string, Record*> &getDefs() const { return Defs; }
576 Record *getClass(const std::string &Name) const {
577 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
578 return I == Classes.end() ? 0 : I->second;
580 Record *getDef(const std::string &Name) const {
581 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
582 return I == Defs.end() ? 0 : I->second;
584 void addClass(Record *R) {
585 assert(getClass(R->getName()) == 0 && "Class already exists!");
586 Classes.insert(std::make_pair(R->getName(), R));
588 void addDef(Record *R) {
589 assert(getDef(R->getName()) == 0 && "Def already exists!");
590 Defs.insert(std::make_pair(R->getName(), R));
596 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
598 extern RecordKeeper Records;