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!");
251 virtual Init *convertInitializerTo(RecTy *Ty) {
252 return Ty->convertValue(this);
254 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
256 virtual bool isComplete() const {
257 for (unsigned i = 0; i != getNumBits(); ++i)
258 if (!getBit(i)->isComplete()) return false;
261 virtual void print(std::ostream &OS) const;
263 virtual Init *resolveReferences(Record &R);
265 // printXX - Print this bitstream with the specified format, returning true if
266 // it is not possible.
267 bool printInHex(std::ostream &OS) const;
268 bool printAsVariable(std::ostream &OS) const;
269 bool printAsUnset(std::ostream &OS) const;
273 /// IntInit - 7 - Represent an initalization by a literal integer value.
275 class IntInit : public Init {
278 IntInit(int V) : Value(V) {}
280 int getValue() const { return Value; }
282 virtual Init *convertInitializerTo(RecTy *Ty) {
283 return Ty->convertValue(this);
285 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
287 virtual void print(std::ostream &OS) const { OS << Value; }
291 /// StringInit - "foo" - Represent an initialization by a string value.
293 class StringInit : public Init {
296 StringInit(const std::string &V) : Value(V) {}
298 virtual Init *convertInitializerTo(RecTy *Ty) {
299 return Ty->convertValue(this);
302 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
305 /// ListInit - [AL, AH, CL] - Represent a list of defs
307 class ListInit : public Init {
308 std::vector<Record*> Records;
310 ListInit(std::vector<Record*> &Rs) {
314 unsigned getSize() const { return Records.size(); }
315 Record *getElement(unsigned i) const {
316 assert(i < Records.size() && "List element index out of range!");
320 virtual Init *convertInitializerTo(RecTy *Ty) {
321 return Ty->convertValue(this);
324 virtual void print(std::ostream &OS) const;
328 /// TypedInit - This is the common super-class of types that have a specific,
331 class TypedInit : public Init {
334 TypedInit(RecTy *T) : Ty(T) {}
336 RecTy *getType() const { return Ty; }
338 /// resolveBitReference - This method is used to implement
339 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
340 /// simply return the resolved value, otherwise we return this.
342 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
345 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
347 class VarInit : public TypedInit {
350 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
352 virtual Init *convertInitializerTo(RecTy *Ty) {
353 return Ty->convertValue(this);
356 const std::string &getName() const { return VarName; }
358 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
360 virtual Init *resolveBitReference(Record &R, unsigned Bit);
362 virtual RecTy *getFieldType(const std::string &FieldName) const;
363 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
365 virtual void print(std::ostream &OS) const { OS << VarName; }
369 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
371 class VarBitInit : public Init {
375 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
376 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
377 ((BitsRecTy*)T->getType())->getNumBits() > B &&
378 "Illegal VarBitInit expression!");
381 virtual Init *convertInitializerTo(RecTy *Ty) {
382 return Ty->convertValue(this);
385 TypedInit *getVariable() const { return TI; }
386 unsigned getBitNum() const { return Bit; }
388 virtual void print(std::ostream &OS) const {
389 TI->print(OS); OS << "{" << Bit << "}";
391 virtual Init *resolveReferences(Record &R);
395 /// DefInit - AL - Represent a reference to a 'def' in the description
397 class DefInit : public Init {
400 DefInit(Record *D) : Def(D) {}
402 virtual Init *convertInitializerTo(RecTy *Ty) {
403 return Ty->convertValue(this);
406 Record *getDef() const { return Def; }
408 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
410 virtual RecTy *getFieldType(const std::string &FieldName) const;
411 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
413 virtual void print(std::ostream &OS) const;
417 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
419 class FieldInit : public TypedInit {
420 Init *Rec; // Record we are referring to
421 std::string FieldName; // Field we are accessing
423 FieldInit(Init *R, const std::string &FN)
424 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
425 assert(getType() && "FieldInit with non-record type!");
428 virtual Init *convertInitializerTo(RecTy *Ty) {
429 return Ty->convertValue(this);
432 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
434 virtual Init *resolveBitReference(Record &R, unsigned Bit);
436 virtual void print(std::ostream &OS) const {
437 Rec->print(OS); OS << "." << FieldName;
442 //===----------------------------------------------------------------------===//
443 // High-Level Classes
444 //===----------------------------------------------------------------------===//
452 RecordVal(const std::string &N, RecTy *T, unsigned P);
454 const std::string &getName() const { return Name; }
456 unsigned getPrefix() const { return Prefix; }
457 RecTy *getType() const { return Ty; }
458 Init *getValue() const { return Value; }
460 bool setValue(Init *V) {
462 Value = V->convertInitializerTo(Ty);
470 void print(std::ostream &OS, bool PrintSem = true) const;
473 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
479 const std::string Name;
480 std::vector<std::string> TemplateArgs;
481 std::vector<RecordVal> Values;
482 std::vector<Record*> SuperClasses;
485 Record(const std::string &N) : Name(N) {}
488 const std::string &getName() const { return Name; }
489 const std::vector<std::string> &getTemplateArgs() const {
492 const std::vector<RecordVal> &getValues() const { return Values; }
493 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
495 bool isTemplateArg(const std::string &Name) const {
496 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
497 if (TemplateArgs[i] == Name) return true;
501 const RecordVal *getValue(const std::string &Name) const {
502 for (unsigned i = 0, e = Values.size(); i != e; ++i)
503 if (Values[i].getName() == Name) return &Values[i];
506 RecordVal *getValue(const std::string &Name) {
507 for (unsigned i = 0, e = Values.size(); i != e; ++i)
508 if (Values[i].getName() == Name) return &Values[i];
512 void addTemplateArg(const std::string &Name) {
513 assert(!isTemplateArg(Name) && "Template arg already defined!");
514 TemplateArgs.push_back(Name);
517 void addValue(const RecordVal &RV) {
518 assert(getValue(RV.getName()) == 0 && "Value already added!");
519 Values.push_back(RV);
522 bool isSubClassOf(Record *R) const {
523 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
524 if (SuperClasses[i] == R)
529 void addSuperClass(Record *R) {
530 assert(!isSubClassOf(R) && "Already subclassing record!");
531 SuperClasses.push_back(R);
534 // resolveReferences - If there are any field references that refer to fields
535 // that have been filled in, we can propagate the values now.
537 void resolveReferences();
542 std::ostream &operator<<(std::ostream &OS, const Record &R);
545 std::map<std::string, Record*> Classes, Defs;
548 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
549 E = Classes.end(); I != E; ++I)
551 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
552 E = Defs.end(); I != E; ++I)
556 const std::map<std::string, Record*> &getClasses() const { return Classes; }
557 const std::map<std::string, Record*> &getDefs() const { return Defs; }
559 Record *getClass(const std::string &Name) const {
560 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
561 return I == Classes.end() ? 0 : I->second;
563 Record *getDef(const std::string &Name) const {
564 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
565 return I == Defs.end() ? 0 : I->second;
567 void addClass(Record *R) {
568 assert(getClass(R->getName()) == 0 && "Class already exists!");
569 Classes.insert(std::make_pair(R->getName(), R));
571 void addDef(Record *R) {
572 assert(getDef(R->getName()) == 0 && "Def already exists!");
573 Defs.insert(std::make_pair(R->getName(), R));
579 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
581 extern RecordKeeper Records;