1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
4 //===----------------------------------------------------------------------===//
26 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
33 virtual Init *convertValue( UnsetInit *UI) { return 0; }
34 virtual Init *convertValue( BitInit *BI) { return 0; }
35 virtual Init *convertValue( BitsInit *BI) { return 0; }
36 virtual Init *convertValue( IntInit *II) { return 0; }
37 virtual Init *convertValue(StringInit *SI) { return 0; }
38 virtual Init *convertValue( ListInit *LI) { return 0; }
39 virtual Init *convertValue( VarInit *VI) { return 0; }
40 virtual Init *convertValue(VarBitInit *VB) { return 0; }
41 virtual Init *convertValue( DefInit *DI) { return 0; }
42 virtual Init *convertValue( FieldInit *FI) { return 0; }
44 virtual void print(std::ostream &OS) const = 0;
48 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
54 /// BitRecTy - 'bit' - Represent a single bit
56 struct BitRecTy : public RecTy {
57 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
58 Init *convertValue(BitInit *BI) { return (Init*)BI; }
59 Init *convertValue(BitsInit *BI);
60 Init *convertValue(IntInit *II);
61 Init *convertValue(VarInit *VI);
62 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
64 void print(std::ostream &OS) const { OS << "bit"; }
68 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
70 class BitsRecTy : public RecTy {
73 BitsRecTy(unsigned Sz) : Size(Sz) {}
75 unsigned getNumBits() const { return Size; }
77 Init *convertValue(UnsetInit *UI);
78 Init *convertValue(BitInit *UI);
79 Init *convertValue(BitsInit *BI);
80 Init *convertValue(IntInit *II);
81 Init *convertValue(VarInit *VI);
82 Init *convertValue(FieldInit *VI);
84 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
88 /// IntRecTy - 'int' - Represent an integer value of no particular size
90 struct IntRecTy : public RecTy {
91 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
92 Init *convertValue(IntInit *II) { return (Init*)II; }
93 Init *convertValue(BitsInit *BI);
94 Init *convertValue(VarInit *VI);
96 void print(std::ostream &OS) const { OS << "int"; }
99 /// StringRecTy - 'string' - Represent an string value
101 struct StringRecTy : public RecTy {
102 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
103 Init *convertValue(StringInit *SI) { return (Init*)SI; }
104 Init *convertValue(VarInit *VI);
105 void print(std::ostream &OS) const { OS << "string"; }
108 /// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
109 /// derived from the specified class.
111 class ListRecTy : public RecTy {
114 ListRecTy(Record *C) : Class(C) {}
115 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
116 Init *convertValue(ListInit *LI);
118 void print(std::ostream &OS) const;
121 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
124 class RecordRecTy : public RecTy {
127 RecordRecTy(Record *R) : Rec(R) {}
129 Record *getRecord() const { return Rec; }
131 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
132 Init *convertValue( DefInit *DI);
134 void print(std::ostream &OS) const;
139 //===----------------------------------------------------------------------===//
140 // Initializer Classes
141 //===----------------------------------------------------------------------===//
146 /// isComplete - This virtual method should be overridden by values that may
147 /// not be completely specified yet.
148 virtual bool isComplete() const { return true; }
150 /// print - Print out this value.
151 virtual void print(std::ostream &OS) const = 0;
153 /// dump - Debugging method that may be called through a debugger, just
154 /// invokes print on cerr.
157 /// convertInitializerTo - This virtual function is a simple call-back
158 /// function that should be overridden to call the appropriate
159 /// RecTy::convertValue method.
161 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
163 /// convertInitializerBitRange - This method is used to implement the bitrange
164 /// selection operator. Given an initializer, it selects the specified bits
165 /// out, returning them as a new init of bits type. If it is not legal to use
166 /// the bit subscript operator on this initializer, return null.
168 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
172 /// getFieldType - This method is used to implement the FieldInit class.
173 /// Implementors of this method should return the type of the named field if
174 /// they are of record type.
176 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
178 /// resolveReferences - This method is used by classes that refer to other
179 /// variables which may not be defined at the time they expression is formed.
180 /// If a value is set for the variable later, this method will be called on
181 /// users of the value to allow the value to propagate out.
183 virtual Init *resolveReferences(Record &R) { return this; }
186 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
187 I.print(OS); return OS;
191 /// UnsetInit - ? - Represents an uninitialized value
193 struct UnsetInit : public Init {
194 virtual Init *convertInitializerTo(RecTy *Ty) {
195 return Ty->convertValue(this);
198 virtual bool isComplete() const { return false; }
199 virtual void print(std::ostream &OS) const { OS << "?"; }
203 /// BitInit - true/false - Represent a concrete initializer for a bit.
205 class BitInit : public Init {
208 BitInit(bool V) : Value(V) {}
210 bool getValue() const { return Value; }
212 virtual Init *convertInitializerTo(RecTy *Ty) {
213 return Ty->convertValue(this);
216 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
219 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
220 /// It contains a vector of bits, whose size is determined by the type.
222 class BitsInit : public Init {
223 std::vector<Init*> Bits;
225 BitsInit(unsigned Size) : Bits(Size) {}
227 unsigned getNumBits() const { return Bits.size(); }
229 Init *getBit(unsigned Bit) const {
230 assert(Bit < Bits.size() && "Bit index out of range!");
233 void setBit(unsigned Bit, Init *V) {
234 assert(Bit < Bits.size() && "Bit index out of range!");
238 virtual Init *convertInitializerTo(RecTy *Ty) {
239 return Ty->convertValue(this);
241 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
243 virtual bool isComplete() const {
244 for (unsigned i = 0; i != getNumBits(); ++i)
245 if (!getBit(i)->isComplete()) return false;
248 virtual void print(std::ostream &OS) const;
250 virtual Init *resolveReferences(Record &R);
252 // printXX - Print this bitstream with the specified format, returning true if
253 // it is not possible.
254 bool printInHex(std::ostream &OS) const;
255 bool printAsVariable(std::ostream &OS) const;
256 bool printAsUnset(std::ostream &OS) const;
260 /// IntInit - 7 - Represent an initalization by a literal integer value.
262 class IntInit : public Init {
265 IntInit(int V) : Value(V) {}
267 int getValue() const { return Value; }
269 virtual Init *convertInitializerTo(RecTy *Ty) {
270 return Ty->convertValue(this);
272 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
274 virtual void print(std::ostream &OS) const { OS << Value; }
278 /// StringInit - "foo" - Represent an initialization by a string value.
280 class StringInit : public Init {
283 StringInit(const std::string &V) : Value(V) {}
285 virtual Init *convertInitializerTo(RecTy *Ty) {
286 return Ty->convertValue(this);
289 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
292 /// ListInit - [AL, AH, CL] - Represent a list of defs
294 class ListInit : public Init {
295 std::vector<Record*> Records;
297 ListInit(std::vector<Record*> &Rs) {
301 unsigned getSize() const { return Records.size(); }
302 Record *getElement(unsigned i) const {
303 assert(i < Records.size() && "List element index out of range!");
307 virtual Init *convertInitializerTo(RecTy *Ty) {
308 return Ty->convertValue(this);
311 virtual void print(std::ostream &OS) const;
315 /// TypedInit - This is the common super-class of types that have a specific,
318 class TypedInit : public Init {
321 TypedInit(RecTy *T) : Ty(T) {}
323 RecTy *getType() const { return Ty; }
325 /// resolveBitReference - This method is used to implement
326 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
327 /// simply return the resolved value, otherwise we return this.
329 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
332 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
334 class VarInit : public TypedInit {
337 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
339 virtual Init *convertInitializerTo(RecTy *Ty) {
340 return Ty->convertValue(this);
343 const std::string &getName() const { return VarName; }
345 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
347 virtual Init *resolveBitReference(Record &R, unsigned Bit);
349 virtual RecTy *getFieldType(const std::string &FieldName) const;
351 virtual void print(std::ostream &OS) const { OS << VarName; }
355 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
357 class VarBitInit : public Init {
361 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
362 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
363 ((BitsRecTy*)T->getType())->getNumBits() > B &&
364 "Illegal VarBitInit expression!");
367 virtual Init *convertInitializerTo(RecTy *Ty) {
368 return Ty->convertValue(this);
371 TypedInit *getVariable() const { return TI; }
372 unsigned getBitNum() const { return Bit; }
374 virtual void print(std::ostream &OS) const {
375 TI->print(OS); OS << "{" << Bit << "}";
377 virtual Init *resolveReferences(Record &R);
381 /// DefInit - AL - Represent a reference to a 'def' in the description
383 class DefInit : public Init {
386 DefInit(Record *D) : Def(D) {}
388 virtual Init *convertInitializerTo(RecTy *Ty) {
389 return Ty->convertValue(this);
392 Record *getDef() const { return Def; }
394 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
396 virtual void print(std::ostream &OS) const;
400 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
402 class FieldInit : public TypedInit {
403 Init *Rec; // Record we are referring to
404 std::string FieldName; // Field we are accessing
406 FieldInit(Init *R, const std::string &FN)
407 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
408 assert(getType() && "FieldInit with non-record type!");
411 virtual Init *convertInitializerTo(RecTy *Ty) {
412 return Ty->convertValue(this);
415 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
417 virtual Init *resolveBitReference(Record &R, unsigned Bit);
419 virtual void print(std::ostream &OS) const {
420 Rec->print(OS); OS << "." << FieldName;
425 //===----------------------------------------------------------------------===//
426 // High-Level Classes
427 //===----------------------------------------------------------------------===//
435 RecordVal(const std::string &N, RecTy *T, unsigned P);
437 const std::string &getName() const { return Name; }
439 unsigned getPrefix() const { return Prefix; }
440 RecTy *getType() const { return Ty; }
441 Init *getValue() const { return Value; }
443 bool setValue(Init *V) {
445 Value = V->convertInitializerTo(Ty);
453 void print(std::ostream &OS, bool PrintSem = true) const;
456 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
462 const std::string Name;
463 std::vector<std::string> TemplateArgs;
464 std::vector<RecordVal> Values;
465 std::vector<Record*> SuperClasses;
468 Record(const std::string &N) : Name(N) {}
471 const std::string &getName() const { return Name; }
472 const std::vector<std::string> &getTemplateArgs() const {
475 const std::vector<RecordVal> &getValues() const { return Values; }
476 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
478 bool isTemplateArg(const std::string &Name) const {
479 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
480 if (TemplateArgs[i] == Name) return true;
484 const RecordVal *getValue(const std::string &Name) const {
485 for (unsigned i = 0, e = Values.size(); i != e; ++i)
486 if (Values[i].getName() == Name) return &Values[i];
489 RecordVal *getValue(const std::string &Name) {
490 for (unsigned i = 0, e = Values.size(); i != e; ++i)
491 if (Values[i].getName() == Name) return &Values[i];
495 void addTemplateArg(const std::string &Name) {
496 assert(!isTemplateArg(Name) && "Template arg already defined!");
497 TemplateArgs.push_back(Name);
500 void addValue(const RecordVal &RV) {
501 assert(getValue(RV.getName()) == 0 && "Value already added!");
502 Values.push_back(RV);
505 bool isSubClassOf(Record *R) const {
506 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
507 if (SuperClasses[i] == R)
512 void addSuperClass(Record *R) {
513 assert(!isSubClassOf(R) && "Already subclassing record!");
514 SuperClasses.push_back(R);
517 // resolveReferences - If there are any field references that refer to fields
518 // that have been filled in, we can propagate the values now.
520 void resolveReferences();
525 std::ostream &operator<<(std::ostream &OS, const Record &R);
528 std::map<std::string, Record*> Classes, Defs;
531 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
532 E = Classes.end(); I != E; ++I)
534 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
535 E = Defs.end(); I != E; ++I)
539 const std::map<std::string, Record*> &getClasses() const { return Classes; }
540 const std::map<std::string, Record*> &getDefs() const { return Defs; }
542 Record *getClass(const std::string &Name) const {
543 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
544 return I == Classes.end() ? 0 : I->second;
546 Record *getDef(const std::string &Name) const {
547 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
548 return I == Defs.end() ? 0 : I->second;
550 void addClass(Record *R) {
551 assert(getClass(R->getName()) == 0 && "Class already exists!");
552 Classes.insert(std::make_pair(R->getName(), R));
554 void addDef(Record *R) {
555 assert(getDef(R->getName()) == 0 && "Def already exists!");
556 Defs.insert(std::make_pair(R->getName(), R));
562 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
564 extern RecordKeeper Records;