1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
4 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
32 virtual Init *convertValue( UnsetInit *UI) { return 0; }
33 virtual Init *convertValue( BitInit *BI) { return 0; }
34 virtual Init *convertValue( BitsInit *BI) { return 0; }
35 virtual Init *convertValue( IntInit *II) { return 0; }
36 virtual Init *convertValue(StringInit *SI) { return 0; }
37 virtual Init *convertValue( ListInit *LI) { return 0; }
38 virtual Init *convertValue( VarInit *VI) { return 0; }
39 virtual Init *convertValue(VarBitInit *VB) { return 0; }
40 virtual Init *convertValue( DefInit *DI) { return 0; }
42 virtual void print(std::ostream &OS) const = 0;
46 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
52 /// BitRecTy - 'bit' - Represent a single bit
54 struct BitRecTy : public RecTy {
55 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
56 Init *convertValue(BitInit *BI) { return (Init*)BI; }
57 Init *convertValue(BitsInit *BI);
58 Init *convertValue(IntInit *II);
59 Init *convertValue(VarInit *VI);
61 void print(std::ostream &OS) const { OS << "bit"; }
65 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
67 class BitsRecTy : public RecTy {
70 BitsRecTy(unsigned Sz) : Size(Sz) {}
72 unsigned getNumBits() const { return Size; }
74 Init *convertValue(UnsetInit *UI);
75 Init *convertValue(BitInit *UI);
76 Init *convertValue(BitsInit *BI);
77 Init *convertValue(IntInit *II);
78 Init *convertValue(VarInit *VI);
80 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
84 /// IntRecTy - 'int' - Represent an integer value of no particular size
86 struct IntRecTy : public RecTy {
87 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
88 Init *convertValue(IntInit *II) { return (Init*)II; }
89 Init *convertValue(BitsInit *BI);
90 Init *convertValue(VarInit *VI);
92 void print(std::ostream &OS) const { OS << "int"; }
95 /// StringRecTy - 'string' - Represent an string value
97 struct StringRecTy : public RecTy {
98 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
99 Init *convertValue(StringInit *SI) { return (Init*)SI; }
100 Init *convertValue(VarInit *VI);
101 void print(std::ostream &OS) const { OS << "string"; }
104 /// ListRecTy - 'list<class>' - Represent a list defs, all of which must be
105 /// derived from the specified class.
107 class ListRecTy : public RecTy {
110 ListRecTy(Record *C) : Class(C) {}
111 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
112 Init *convertValue(ListInit *LI);
114 void print(std::ostream &OS) const;
117 /// RecordRecTy - '<classname>' - Represent an instance of a class, such as:
120 class RecordRecTy : public RecTy {
123 RecordRecTy(Record *R) : Rec(R) {}
125 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
126 Init *convertValue( DefInit *DI);
128 void print(std::ostream &OS) const;
133 //===----------------------------------------------------------------------===//
134 // Initializer Classes
135 //===----------------------------------------------------------------------===//
140 virtual bool isComplete() const = 0;
141 virtual void print(std::ostream &OS) const = 0;
144 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
145 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
149 virtual Init *resolveReferences(Record &R) { return this; }
152 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
153 I.print(OS); return OS;
157 /// UnsetInit - ? - Represents an uninitialized value
159 struct UnsetInit : public Init {
160 virtual Init *convertInitializerTo(RecTy *Ty) {
161 return Ty->convertValue(this);
164 virtual bool isComplete() const { return false; }
165 virtual void print(std::ostream &OS) const { OS << "?"; }
169 /// BitInit - true/false - Represent a concrete initializer for a bit.
171 class BitInit : public Init {
174 BitInit(bool V) : Value(V) {}
176 bool getValue() const { return Value; }
178 virtual Init *convertInitializerTo(RecTy *Ty) {
179 return Ty->convertValue(this);
182 virtual bool isComplete() const { return true; }
183 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
186 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
187 /// It contains a vector of bits, whose size is determined by the type.
189 class BitsInit : public Init {
190 std::vector<Init*> Bits;
192 BitsInit(unsigned Size) : Bits(Size) {}
194 unsigned getNumBits() const { return Bits.size(); }
196 Init *getBit(unsigned Bit) const {
197 assert(Bit < Bits.size() && "Bit index out of range!");
200 void setBit(unsigned Bit, Init *V) {
201 assert(Bit < Bits.size() && "Bit index out of range!");
205 virtual Init *convertInitializerTo(RecTy *Ty) {
206 return Ty->convertValue(this);
208 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
210 virtual bool isComplete() const {
211 for (unsigned i = 0; i != getNumBits(); ++i)
212 if (!getBit(i)->isComplete()) return false;
215 virtual void print(std::ostream &OS) const;
217 virtual Init *resolveReferences(Record &R);
219 // printXX - Print this bitstream with the specified format, returning true if
220 // it is not possible.
221 bool printInHex(std::ostream &OS) const;
222 bool printAsVariable(std::ostream &OS) const;
223 bool printAsUnset(std::ostream &OS) const;
227 /// IntInit - 7 - Represent an initalization by a literal integer value.
229 class IntInit : public Init {
232 IntInit(int V) : Value(V) {}
234 int getValue() const { return Value; }
236 virtual Init *convertInitializerTo(RecTy *Ty) {
237 return Ty->convertValue(this);
239 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
241 virtual bool isComplete() const { return true; }
242 virtual void print(std::ostream &OS) const { OS << Value; }
246 /// StringInit - "foo" - Represent an initialization by a string value.
248 class StringInit : public Init {
251 StringInit(const std::string &V) : Value(V) {}
253 virtual Init *convertInitializerTo(RecTy *Ty) {
254 return Ty->convertValue(this);
257 virtual bool isComplete() const { return true; }
258 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
261 /// ListInit - [AL, AH, CL] - Represent a list of defs
263 class ListInit : public Init {
264 std::vector<Record*> Records;
266 ListInit(std::vector<Record*> &Rs) {
270 unsigned getSize() const { return Records.size(); }
271 Record *getElement(unsigned i) const {
272 assert(i < Records.size() && "List element index out of range!");
276 virtual Init *convertInitializerTo(RecTy *Ty) {
277 return Ty->convertValue(this);
280 virtual bool isComplete() const { return true; }
281 virtual void print(std::ostream &OS) const;
284 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
286 class VarInit : public Init {
290 VarInit(const std::string &VN, RecTy *T) : VarName(VN), Ty(T) {}
292 virtual Init *convertInitializerTo(RecTy *Ty) {
293 return Ty->convertValue(this);
296 const std::string &getName() const { return VarName; }
297 RecTy *getType() const { return Ty; }
299 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
301 virtual bool isComplete() const { return true; }
302 virtual void print(std::ostream &OS) const { OS << VarName; }
306 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable
308 class VarBitInit : public Init {
312 VarBitInit(VarInit *V, unsigned B) : VI(V), Bit(B) {}
314 virtual Init *convertInitializerTo(RecTy *Ty) {
315 return Ty->convertValue(this);
318 VarInit *getVariable() const { return VI; }
319 unsigned getBitNum() const { return Bit; }
321 virtual bool isComplete() const { return true; }
322 virtual void print(std::ostream &OS) const {
323 VI->print(OS); OS << "{" << Bit << "}";
325 virtual Init *resolveReferences(Record &R);
329 /// DefInit - AL - Represent a reference to a 'def' in the description
331 class DefInit : public Init {
334 DefInit(Record *D) : Def(D) {}
336 virtual Init *convertInitializerTo(RecTy *Ty) {
337 return Ty->convertValue(this);
340 Record *getDef() const { return Def; }
342 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
344 virtual bool isComplete() const { return true; }
345 virtual void print(std::ostream &OS) const;
350 //===----------------------------------------------------------------------===//
351 // High-Level Classes
352 //===----------------------------------------------------------------------===//
360 RecordVal(const std::string &N, RecTy *T, unsigned P);
361 ~RecordVal() { /*delete Ty; delete Value; Bad for copy ctor!*/ }
363 const std::string &getName() const { return Name; }
365 unsigned getPrefix() const { return Prefix; }
366 RecTy *getType() const { return Ty; }
367 Init *getValue() const { return Value; }
369 bool setValue(Init *V) {
371 Value = V->convertInitializerTo(Ty);
379 void print(std::ostream &OS, bool PrintSem = true) const;
382 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
388 const std::string Name;
389 std::vector<std::string> TemplateArgs;
390 std::vector<RecordVal> Values;
391 std::vector<Record*> SuperClasses;
394 Record(const std::string &N) : Name(N) {}
397 const std::string &getName() const { return Name; }
398 const std::vector<std::string> &getTemplateArgs() const {
401 const std::vector<RecordVal> &getValues() const { return Values; }
402 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
404 bool isTemplateArg(const std::string &Name) const {
405 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
406 if (TemplateArgs[i] == Name) return true;
410 const RecordVal *getValue(const std::string &Name) const {
411 for (unsigned i = 0, e = Values.size(); i != e; ++i)
412 if (Values[i].getName() == Name) return &Values[i];
415 RecordVal *getValue(const std::string &Name) {
416 for (unsigned i = 0, e = Values.size(); i != e; ++i)
417 if (Values[i].getName() == Name) return &Values[i];
421 void addTemplateArg(const std::string &Name) {
422 assert(!isTemplateArg(Name) && "Template arg already defined!");
423 TemplateArgs.push_back(Name);
426 void addValue(const RecordVal &RV) {
427 assert(getValue(RV.getName()) == 0 && "Value already added!");
428 Values.push_back(RV);
431 bool isSubClassOf(Record *R) const {
432 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
433 if (SuperClasses[i] == R)
438 void addSuperClass(Record *R) {
439 assert(!isSubClassOf(R) && "Already subclassing record!");
440 SuperClasses.push_back(R);
443 // resolveReferences - If there are any field references that refer to fields
444 // that have been filled in, we can propagate the values now.
446 void resolveReferences();
451 std::ostream &operator<<(std::ostream &OS, const Record &R);
454 std::map<std::string, Record*> Classes, Defs;
457 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
458 E = Classes.end(); I != E; ++I)
460 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
461 E = Defs.end(); I != E; ++I)
465 const std::map<std::string, Record*> &getClasses() const { return Classes; }
466 const std::map<std::string, Record*> &getDefs() const { return Defs; }
468 Record *getClass(const std::string &Name) const {
469 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
470 return I == Classes.end() ? 0 : I->second;
472 Record *getDef(const std::string &Name) const {
473 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
474 return I == Defs.end() ? 0 : I->second;
476 void addClass(Record *R) {
477 assert(getClass(R->getName()) == 0 && "Class already exists!");
478 Classes.insert(std::make_pair(R->getName(), R));
480 void addDef(Record *R) {
481 assert(getDef(R->getName()) == 0 && "Def already exists!");
482 Defs.insert(std::make_pair(R->getName(), R));
488 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
490 extern RecordKeeper Records;