1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
4 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
37 virtual Init *convertValue( UnsetInit *UI) { return 0; }
38 virtual Init *convertValue( BitInit *BI) { return 0; }
39 virtual Init *convertValue( BitsInit *BI) { return 0; }
40 virtual Init *convertValue( IntInit *II) { return 0; }
41 virtual Init *convertValue(StringInit *SI) { return 0; }
42 virtual Init *convertValue( ListInit *LI) { return 0; }
43 virtual Init *convertValue( CodeInit *CI) { return 0; }
44 virtual Init *convertValue(VarBitInit *VB) { return 0; }
45 virtual Init *convertValue( DefInit *DI) { return 0; }
46 virtual Init *convertValue( TypedInit *TI) { return 0; }
47 virtual Init *convertValue( VarInit *VI) {
48 return convertValue((TypedInit*)VI);
50 virtual Init *convertValue( FieldInit *FI) {
51 return convertValue((TypedInit*)FI);
54 virtual void print(std::ostream &OS) const = 0;
58 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
64 /// BitRecTy - 'bit' - Represent a single bit
66 struct BitRecTy : public RecTy {
67 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
68 Init *convertValue(BitInit *BI) { return (Init*)BI; }
69 Init *convertValue(BitsInit *BI);
70 Init *convertValue(IntInit *II);
71 Init *convertValue(TypedInit *VI);
72 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
74 void print(std::ostream &OS) const { OS << "bit"; }
78 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
80 class BitsRecTy : public RecTy {
83 BitsRecTy(unsigned Sz) : Size(Sz) {}
85 unsigned getNumBits() const { return Size; }
87 Init *convertValue(UnsetInit *UI);
88 Init *convertValue(BitInit *UI);
89 Init *convertValue(BitsInit *BI);
90 Init *convertValue(IntInit *II);
91 Init *convertValue(TypedInit *VI);
93 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
97 /// IntRecTy - 'int' - Represent an integer value of no particular size
99 struct IntRecTy : public RecTy {
100 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
101 Init *convertValue(IntInit *II) { return (Init*)II; }
102 Init *convertValue(BitsInit *BI);
103 Init *convertValue(TypedInit *TI);
105 void print(std::ostream &OS) const { OS << "int"; }
108 /// StringRecTy - 'string' - Represent an string value
110 struct StringRecTy : public RecTy {
111 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
112 Init *convertValue(StringInit *SI) { return (Init*)SI; }
113 Init *convertValue(TypedInit *TI);
114 void print(std::ostream &OS) const { OS << "string"; }
117 /// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
118 /// derived from the specified class.
120 class ListRecTy : public RecTy {
123 ListRecTy(Record *C) : Class(C) {}
125 /// getElementClass - Return the class that the list contains.
127 Record *getElementClass() const { return Class; }
129 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
130 Init *convertValue(ListInit *LI);
131 Init *convertValue(TypedInit *TI);
133 void print(std::ostream &OS) const;
136 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
138 struct CodeRecTy : public RecTy {
139 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
140 Init *convertValue( CodeInit *CI) { return (Init*)CI; }
142 void print(std::ostream &OS) const { OS << "code"; }
146 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
149 class RecordRecTy : public RecTy {
152 RecordRecTy(Record *R) : Rec(R) {}
154 Record *getRecord() const { return Rec; }
156 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
157 Init *convertValue( DefInit *DI);
158 Init *convertValue(TypedInit *VI);
160 void print(std::ostream &OS) const;
165 //===----------------------------------------------------------------------===//
166 // Initializer Classes
167 //===----------------------------------------------------------------------===//
172 /// isComplete - This virtual method should be overridden by values that may
173 /// not be completely specified yet.
174 virtual bool isComplete() const { return true; }
176 /// print - Print out this value.
177 virtual void print(std::ostream &OS) const = 0;
179 /// dump - Debugging method that may be called through a debugger, just
180 /// invokes print on cerr.
183 /// convertInitializerTo - This virtual function is a simple call-back
184 /// function that should be overridden to call the appropriate
185 /// RecTy::convertValue method.
187 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
189 /// convertInitializerBitRange - This method is used to implement the bitrange
190 /// selection operator. Given an initializer, it selects the specified bits
191 /// out, returning them as a new init of bits type. If it is not legal to use
192 /// the bit subscript operator on this initializer, return null.
194 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
198 /// getFieldType - This method is used to implement the FieldInit class.
199 /// Implementors of this method should return the type of the named field if
200 /// they are of record type.
202 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
204 /// getFieldInit - This method complements getFieldType to return the
205 /// initializer for the specified field. If getFieldType returns non-null
206 /// this method should return non-null, otherwise it returns null.
208 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
212 /// resolveReferences - This method is used by classes that refer to other
213 /// variables which may not be defined at the time they expression is formed.
214 /// If a value is set for the variable later, this method will be called on
215 /// users of the value to allow the value to propagate out.
217 virtual Init *resolveReferences(Record &R) { return this; }
220 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
221 I.print(OS); return OS;
225 /// UnsetInit - ? - Represents an uninitialized value
227 struct UnsetInit : public Init {
228 virtual Init *convertInitializerTo(RecTy *Ty) {
229 return Ty->convertValue(this);
232 virtual bool isComplete() const { return false; }
233 virtual void print(std::ostream &OS) const { OS << "?"; }
237 /// BitInit - true/false - Represent a concrete initializer for a bit.
239 class BitInit : public Init {
242 BitInit(bool V) : Value(V) {}
244 bool getValue() const { return Value; }
246 virtual Init *convertInitializerTo(RecTy *Ty) {
247 return Ty->convertValue(this);
250 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
253 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
254 /// It contains a vector of bits, whose size is determined by the type.
256 class BitsInit : public Init {
257 std::vector<Init*> Bits;
259 BitsInit(unsigned Size) : Bits(Size) {}
261 unsigned getNumBits() const { return Bits.size(); }
263 Init *getBit(unsigned Bit) const {
264 assert(Bit < Bits.size() && "Bit index out of range!");
267 void setBit(unsigned Bit, Init *V) {
268 assert(Bit < Bits.size() && "Bit index out of range!");
269 assert(Bits[Bit] == 0 && "Bit already set!");
273 virtual Init *convertInitializerTo(RecTy *Ty) {
274 return Ty->convertValue(this);
276 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
278 virtual bool isComplete() const {
279 for (unsigned i = 0; i != getNumBits(); ++i)
280 if (!getBit(i)->isComplete()) return false;
283 virtual void print(std::ostream &OS) const;
285 virtual Init *resolveReferences(Record &R);
287 // printXX - Print this bitstream with the specified format, returning true if
288 // it is not possible.
289 bool printInHex(std::ostream &OS) const;
290 bool printAsVariable(std::ostream &OS) const;
291 bool printAsUnset(std::ostream &OS) const;
295 /// IntInit - 7 - Represent an initalization by a literal integer value.
297 class IntInit : public Init {
300 IntInit(int V) : Value(V) {}
302 int getValue() const { return Value; }
304 virtual Init *convertInitializerTo(RecTy *Ty) {
305 return Ty->convertValue(this);
307 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
309 virtual void print(std::ostream &OS) const { OS << Value; }
313 /// StringInit - "foo" - Represent an initialization by a string value.
315 class StringInit : public Init {
318 StringInit(const std::string &V) : Value(V) {}
320 virtual Init *convertInitializerTo(RecTy *Ty) {
321 return Ty->convertValue(this);
324 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
327 /// CodeInit - "[{...}]" - Represent a code fragment.
329 class CodeInit : public Init {
332 CodeInit(const std::string &V) : Value(V) {}
334 virtual Init *convertInitializerTo(RecTy *Ty) {
335 return Ty->convertValue(this);
338 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
341 /// ListInit - [AL, AH, CL] - Represent a list of defs
343 class ListInit : public Init {
344 std::vector<Record*> Records;
346 ListInit(std::vector<Record*> &Rs) {
350 unsigned getSize() const { return Records.size(); }
351 Record *getElement(unsigned i) const {
352 assert(i < Records.size() && "List element index out of range!");
356 virtual Init *convertInitializerTo(RecTy *Ty) {
357 return Ty->convertValue(this);
360 virtual void print(std::ostream &OS) const;
364 /// TypedInit - This is the common super-class of types that have a specific,
367 class TypedInit : public Init {
370 TypedInit(RecTy *T) : Ty(T) {}
372 RecTy *getType() const { return Ty; }
374 /// resolveBitReference - This method is used to implement
375 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
376 /// simply return the resolved value, otherwise we return this.
378 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
381 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
383 class VarInit : public TypedInit {
386 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
388 virtual Init *convertInitializerTo(RecTy *Ty) {
389 return Ty->convertValue(this);
392 const std::string &getName() const { return VarName; }
394 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
396 virtual Init *resolveBitReference(Record &R, unsigned Bit);
398 virtual RecTy *getFieldType(const std::string &FieldName) const;
399 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
401 /// resolveReferences - This method is used by classes that refer to other
402 /// variables which may not be defined at the time they expression is formed.
403 /// If a value is set for the variable later, this method will be called on
404 /// users of the value to allow the value to propagate out.
406 virtual Init *resolveReferences(Record &R);
408 virtual void print(std::ostream &OS) const { OS << VarName; }
412 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
414 class VarBitInit : public Init {
418 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
419 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
420 ((BitsRecTy*)T->getType())->getNumBits() > B &&
421 "Illegal VarBitInit expression!");
424 virtual Init *convertInitializerTo(RecTy *Ty) {
425 return Ty->convertValue(this);
428 TypedInit *getVariable() const { return TI; }
429 unsigned getBitNum() const { return Bit; }
431 virtual void print(std::ostream &OS) const {
432 TI->print(OS); OS << "{" << Bit << "}";
434 virtual Init *resolveReferences(Record &R);
438 /// DefInit - AL - Represent a reference to a 'def' in the description
440 class DefInit : public Init {
443 DefInit(Record *D) : Def(D) {}
445 virtual Init *convertInitializerTo(RecTy *Ty) {
446 return Ty->convertValue(this);
449 Record *getDef() const { return Def; }
451 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
453 virtual RecTy *getFieldType(const std::string &FieldName) const;
454 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
456 virtual void print(std::ostream &OS) const;
460 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
462 class FieldInit : public TypedInit {
463 Init *Rec; // Record we are referring to
464 std::string FieldName; // Field we are accessing
466 FieldInit(Init *R, const std::string &FN)
467 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
468 assert(getType() && "FieldInit with non-record type!");
471 virtual Init *convertInitializerTo(RecTy *Ty) {
472 return Ty->convertValue(this);
475 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
477 virtual Init *resolveBitReference(Record &R, unsigned Bit);
479 virtual void print(std::ostream &OS) const {
480 Rec->print(OS); OS << "." << FieldName;
485 //===----------------------------------------------------------------------===//
486 // High-Level Classes
487 //===----------------------------------------------------------------------===//
495 RecordVal(const std::string &N, RecTy *T, unsigned P);
497 const std::string &getName() const { return Name; }
499 unsigned getPrefix() const { return Prefix; }
500 RecTy *getType() const { return Ty; }
501 Init *getValue() const { return Value; }
503 bool setValue(Init *V) {
505 Value = V->convertInitializerTo(Ty);
513 void print(std::ostream &OS, bool PrintSem = true) const;
516 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
522 const std::string Name;
523 std::vector<std::string> TemplateArgs;
524 std::vector<RecordVal> Values;
525 std::vector<Record*> SuperClasses;
528 Record(const std::string &N) : Name(N) {}
531 const std::string &getName() const { return Name; }
532 const std::vector<std::string> &getTemplateArgs() const {
535 const std::vector<RecordVal> &getValues() const { return Values; }
536 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
538 bool isTemplateArg(const std::string &Name) const {
539 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
540 if (TemplateArgs[i] == Name) return true;
544 const RecordVal *getValue(const std::string &Name) const {
545 for (unsigned i = 0, e = Values.size(); i != e; ++i)
546 if (Values[i].getName() == Name) return &Values[i];
549 RecordVal *getValue(const std::string &Name) {
550 for (unsigned i = 0, e = Values.size(); i != e; ++i)
551 if (Values[i].getName() == Name) return &Values[i];
555 void addTemplateArg(const std::string &Name) {
556 assert(!isTemplateArg(Name) && "Template arg already defined!");
557 TemplateArgs.push_back(Name);
560 void addValue(const RecordVal &RV) {
561 assert(getValue(RV.getName()) == 0 && "Value already added!");
562 Values.push_back(RV);
565 void removeValue(const std::string &Name) {
566 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
567 for (unsigned i = 0, e = Values.size(); i != e; ++i)
568 if (Values[i].getName() == Name) {
569 Values.erase(Values.begin()+i);
572 assert(0 && "Name does not exist in record!");
575 bool isSubClassOf(Record *R) const {
576 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
577 if (SuperClasses[i] == R)
582 void addSuperClass(Record *R) {
583 assert(!isSubClassOf(R) && "Already subclassing record!");
584 SuperClasses.push_back(R);
587 // resolveReferences - If there are any field references that refer to fields
588 // that have been filled in, we can propagate the values now.
590 void resolveReferences();
595 std::ostream &operator<<(std::ostream &OS, const Record &R);
598 std::map<std::string, Record*> Classes, Defs;
601 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
602 E = Classes.end(); I != E; ++I)
604 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
605 E = Defs.end(); I != E; ++I)
609 const std::map<std::string, Record*> &getClasses() const { return Classes; }
610 const std::map<std::string, Record*> &getDefs() const { return Defs; }
612 Record *getClass(const std::string &Name) const {
613 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
614 return I == Classes.end() ? 0 : I->second;
616 Record *getDef(const std::string &Name) const {
617 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
618 return I == Defs.end() ? 0 : I->second;
620 void addClass(Record *R) {
621 assert(getClass(R->getName()) == 0 && "Class already exists!");
622 Classes.insert(std::make_pair(R->getName(), R));
624 void addDef(Record *R) {
625 assert(getDef(R->getName()) == 0 && "Def already exists!");
626 Defs.insert(std::make_pair(R->getName(), R));
632 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
634 extern RecordKeeper Records;