blob: 384005081e707668ea52264dce35857fc4258843 [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- Record.cpp - Record implementation ---------------------------------===//
2//
3//
4//===----------------------------------------------------------------------===//
5
6#include "Record.h"
7
8//===----------------------------------------------------------------------===//
9// Type implementations
10//===----------------------------------------------------------------------===//
11
12void RecTy::dump() const { print(std::cerr); }
13
14Init *BitRecTy::convertValue(BitsInit *BI) {
15 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
16 return BI->getBit(0);
17}
18
19bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
20 return RHS->getNumBits() == 1;
21}
22
23Init *BitRecTy::convertValue(IntInit *II) {
24 int Val = II->getValue();
25 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
26
27 return new BitInit(Val != 0);
28}
29
30Init *BitRecTy::convertValue(TypedInit *VI) {
31 if (dynamic_cast<BitRecTy*>(VI->getType()))
32 return VI; // Accept variable if it is already of bit type!
33 return 0;
34}
35
36Init *BitsRecTy::convertValue(UnsetInit *UI) {
37 BitsInit *Ret = new BitsInit(Size);
38
39 for (unsigned i = 0; i != Size; ++i)
40 Ret->setBit(i, new UnsetInit());
41 return Ret;
42}
43
44Init *BitsRecTy::convertValue(BitInit *UI) {
45 if (Size != 1) return 0; // Can only convert single bit...
46 BitsInit *Ret = new BitsInit(1);
47 Ret->setBit(0, UI);
48 return Ret;
49}
50
51// convertValue from Int initializer to bits type: Split the integer up into the
52// appropriate bits...
53//
54Init *BitsRecTy::convertValue(IntInit *II) {
55 int Value = II->getValue();
56 // Make sure this bitfield is large enough to hold the integer value...
57 if (Value >= 0) {
58 if (Value & ~((1 << Size)-1))
59 return 0;
60 } else {
61 if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
62 return 0;
63 }
64
65 BitsInit *Ret = new BitsInit(Size);
66 for (unsigned i = 0; i != Size; ++i)
67 Ret->setBit(i, new BitInit(Value & (1 << i)));
68
69 return Ret;
70}
71
72Init *BitsRecTy::convertValue(BitsInit *BI) {
73 // If the number of bits is right, return it. Otherwise we need to expand or
74 // truncate...
75 if (BI->getNumBits() == Size) return BI;
76 return 0;
77}
78
79Init *BitsRecTy::convertValue(TypedInit *VI) {
80 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
81 if (BRT->Size == Size) {
82 BitsInit *Ret = new BitsInit(Size);
83 for (unsigned i = 0; i != Size; ++i)
84 Ret->setBit(i, new VarBitInit(VI, i));
85 return Ret;
86 }
87 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
88 BitsInit *Ret = new BitsInit(1);
89 Ret->setBit(0, VI);
90 return Ret;
91 }
92
93 return 0;
94}
95
96Init *IntRecTy::convertValue(BitInit *BI) {
97 return new IntInit(BI->getValue());
98}
99
100Init *IntRecTy::convertValue(BitsInit *BI) {
101 int Result = 0;
102 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
103 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
104 Result |= Bit->getValue() << i;
105 } else {
106 return 0;
107 }
108 return new IntInit(Result);
109}
110
111Init *IntRecTy::convertValue(TypedInit *TI) {
112 if (TI->getType()->typeIsConvertibleTo(this))
113 return TI; // Accept variable if already of the right type!
114 return 0;
115}
116
117Init *StringRecTy::convertValue(TypedInit *TI) {
118 if (dynamic_cast<StringRecTy*>(TI->getType()))
119 return TI; // Accept variable if already of the right type!
120 return 0;
121}
122
123void ListRecTy::print(std::ostream &OS) const {
124 OS << "list<" << *Ty << ">";
125}
126
127Init *ListRecTy::convertValue(ListInit *LI) {
128 std::vector<Init*> Elements;
129
130 // Verify that all of the elements of the list are subclasses of the
131 // appropriate class!
132 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
133 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
134 Elements.push_back(CI);
135 else
136 return 0;
137
138 return new ListInit(Elements);
139}
140
141Init *ListRecTy::convertValue(TypedInit *TI) {
142 // Ensure that TI is compatible with our class.
143 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
144 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
145 return TI;
146 return 0;
147}
148
149Init *CodeRecTy::convertValue(TypedInit *TI) {
150 if (TI->getType()->typeIsConvertibleTo(this))
151 return TI;
152 return 0;
153}
154
155Init *DagRecTy::convertValue(TypedInit *TI) {
156 if (TI->getType()->typeIsConvertibleTo(this))
157 return TI;
158 return 0;
159}
160
161
162void RecordRecTy::print(std::ostream &OS) const {
163 OS << Rec->getName();
164}
165
166Init *RecordRecTy::convertValue(DefInit *DI) {
167 // Ensure that DI is a subclass of Rec.
168 if (!DI->getDef()->isSubClassOf(Rec))
169 return 0;
170 return DI;
171}
172
173Init *RecordRecTy::convertValue(TypedInit *TI) {
174 // Ensure that TI is compatible with Rec.
175 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
176 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
177 RRT->getRecord() == getRecord())
178 return TI;
179 return 0;
180}
181
182bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
183 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
184}
185
186
187//===----------------------------------------------------------------------===//
188// Initializer implementations
189//===----------------------------------------------------------------------===//
190
191void Init::dump() const { return print(std::cerr); }
192
193Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
194 BitsInit *BI = new BitsInit(Bits.size());
195 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
196 if (Bits[i] >= getNumBits()) {
197 delete BI;
198 return 0;
199 }
200 BI->setBit(i, getBit(Bits[i]));
201 }
202 return BI;
203}
204
205void BitsInit::print(std::ostream &OS) const {
206 //if (!printInHex(OS)) return;
207 //if (!printAsVariable(OS)) return;
208 //if (!printAsUnset(OS)) return;
209
210 OS << "{ ";
211 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
212 if (i) OS << ", ";
213 if (Init *Bit = getBit(e-i-1))
214 Bit->print(OS);
215 else
216 OS << "*";
217 }
218 OS << " }";
219}
220
221bool BitsInit::printInHex(std::ostream &OS) const {
222 // First, attempt to convert the value into an integer value...
223 int Result = 0;
224 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
225 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
226 Result |= Bit->getValue() << i;
227 } else {
228 return true;
229 }
230
231 OS << "0x" << std::hex << Result << std::dec;
232 return false;
233}
234
235bool BitsInit::printAsVariable(std::ostream &OS) const {
236 // Get the variable that we may be set equal to...
237 assert(getNumBits() != 0);
238 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
239 if (FirstBit == 0) return true;
240 TypedInit *Var = FirstBit->getVariable();
241
242 // Check to make sure the types are compatible.
243 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
244 if (Ty == 0) return true;
245 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
246
247 // Check to make sure all bits are referring to the right bits in the variable
248 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
249 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
250 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
251 return true;
252 }
253
254 Var->print(OS);
255 return false;
256}
257
258bool BitsInit::printAsUnset(std::ostream &OS) const {
259 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
260 if (!dynamic_cast<UnsetInit*>(getBit(i)))
261 return true;
262 OS << "?";
263 return false;
264}
265
266// resolveReferences - If there are any field references that refer to fields
267// that have been filled in, we can propagate the values now.
268//
269Init *BitsInit::resolveReferences(Record &R) {
270 bool Changed = false;
271 BitsInit *New = new BitsInit(getNumBits());
272
273 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
274 Init *B;
275 Init *CurBit = getBit(i);
276
277 do {
278 B = CurBit;
279 CurBit = CurBit->resolveReferences(R);
280 Changed |= B != CurBit;
281 } while (B != CurBit);
282 New->setBit(i, CurBit);
283 }
284
285 if (Changed)
286 return New;
287 delete New;
288 return this;
289}
290
291Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
292 BitsInit *BI = new BitsInit(Bits.size());
293
294 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
295 if (Bits[i] >= 32) {
296 delete BI;
297 return 0;
298 }
299 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
300 }
301 return BI;
302}
303
304void ListInit::print(std::ostream &OS) const {
305 OS << "[";
306 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
307 if (i) OS << ", ";
308 OS << *Values[i];
309 }
310 OS << "]";
311}
312
313Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
314 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
315 if (T == 0) return 0; // Cannot subscript a non-bits variable...
316 unsigned NumBits = T->getNumBits();
317
318 BitsInit *BI = new BitsInit(Bits.size());
319 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
320 if (Bits[i] >= NumBits) {
321 delete BI;
322 return 0;
323 }
324 BI->setBit(i, new VarBitInit(this, Bits[i]));
325 }
326 return BI;
327}
328
329Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
330 if (R.isTemplateArg(getName()))
331 return this;
332
333 RecordVal *RV = R.getValue(getName());
334 assert(RV && "Reference to a non-existant variable?");
335 assert(dynamic_cast<BitsInit*>(RV->getValue()));
336 BitsInit *BI = (BitsInit*)RV->getValue();
337
338 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
339 Init *B = BI->getBit(Bit);
340
341 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
342 return B; // Replace the VarBitInit with it.
343 return this;
344}
345
346RecTy *VarInit::getFieldType(const std::string &FieldName) const {
347 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
348 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
349 return RV->getType();
350 return 0;
351}
352
353Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
354 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
355 if (const RecordVal *RV = R.getValue(VarName))
356 if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
357 return I;
358 else
359 return 0;
360 return 0;
361}
362
363/// resolveReferences - This method is used by classes that refer to other
364/// variables which may not be defined at the time they expression is formed.
365/// If a value is set for the variable later, this method will be called on
366/// users of the value to allow the value to propagate out.
367///
368Init *VarInit::resolveReferences(Record &R) {
369 if (RecordVal *Val = R.getValue(VarName))
370 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
371 return Val->getValue();
372 return this;
373}
374
375
376Init *VarBitInit::resolveReferences(Record &R) {
377 Init *I = getVariable()->resolveBitReference(R, getBitNum());
378 if (I != getVariable())
379 return I;
380 return this;
381}
382
383RecTy *DefInit::getFieldType(const std::string &FieldName) const {
384 if (const RecordVal *RV = Def->getValue(FieldName))
385 return RV->getType();
386 return 0;
387}
388
389Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
390 return Def->getValue(FieldName)->getValue();
391}
392
393
394void DefInit::print(std::ostream &OS) const {
395 OS << Def->getName();
396}
397
398Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
399 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
400 if (T == 0) return 0; // Cannot subscript a non-bits field...
401 unsigned NumBits = T->getNumBits();
402
403 BitsInit *BI = new BitsInit(Bits.size());
404 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
405 if (Bits[i] >= NumBits) {
406 delete BI;
407 return 0;
408 }
409 BI->setBit(i, new VarBitInit(this, Bits[i]));
410 }
411 return BI;
412}
413
414Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
415 Init *BitsVal = Rec->getFieldInit(R, FieldName);
416 if (BitsVal)
417 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
418 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
419 Init *B = BI->getBit(Bit);
420
421 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
422 return B; // Replace the VarBitInit with it.
423 }
424 return this;
425}
426
427Init *FieldInit::resolveReferences(Record &R) {
428 Init *BitsVal = Rec->getFieldInit(R, FieldName);
429 if (BitsVal) {
430 Init *BVR = BitsVal->resolveReferences(R);
431 return BVR->isComplete() ? BVR : this;
432 }
433 return this;
434}
435
436
437void DagInit::print(std::ostream &OS) const {
438 OS << "(" << NodeTypeDef->getName();
439 if (Args.size()) {
440 OS << " " << *Args[0];
441 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
442 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
443 OS << ", " << *Args[i];
444 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
445 }
446 }
447 OS << ")";
448}
449
450
451//===----------------------------------------------------------------------===//
452// Other implementations
453//===----------------------------------------------------------------------===//
454
455RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
456 : Name(N), Ty(T), Prefix(P) {
457 Value = Ty->convertValue(new UnsetInit());
458 assert(Value && "Cannot create unset value for current type!");
459}
460
461void RecordVal::dump() const { std::cerr << *this; }
462
463void RecordVal::print(std::ostream &OS, bool PrintSem) const {
464 if (getPrefix()) OS << "field ";
465 OS << *getType() << " " << getName();
466 if (getValue()) {
467 OS << " = " << *getValue();
468 }
469 if (PrintSem) OS << ";\n";
470}
471
472// resolveReferences - If there are any field references that refer to fields
473// that have been filled in, we can propagate the values now.
474//
475void Record::resolveReferences() {
476 for (unsigned i = 0, e = Values.size(); i != e; ++i)
477 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
478}
479
480void Record::dump() const { std::cerr << *this; }
481
482std::ostream &operator<<(std::ostream &OS, const Record &R) {
483 OS << R.getName();
484
485 const std::vector<std::string> &TArgs = R.getTemplateArgs();
486 if (!TArgs.empty()) {
487 OS << "<";
488 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
489 if (i) OS << ", ";
490 const RecordVal *RV = R.getValue(TArgs[i]);
491 assert(RV && "Template argument record not found??");
492 RV->print(OS, false);
493 }
494 OS << ">";
495 }
496
497 OS << " {";
498 const std::vector<Record*> &SC = R.getSuperClasses();
499 if (!SC.empty()) {
500 OS << "\t//";
501 for (unsigned i = 0, e = SC.size(); i != e; ++i)
502 OS << " " << SC[i]->getName();
503 }
504 OS << "\n";
505
506 const std::vector<RecordVal> &Vals = R.getValues();
507 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
508 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
509 OS << Vals[i];
510 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
511 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
512 OS << Vals[i];
513
514 return OS << "}\n";
515}
516
517/// getValueInit - Return the initializer for a value with the specified name,
518/// or throw an exception if the field does not exist.
519///
520Init *Record::getValueInit(const std::string &FieldName) const {
521 const RecordVal *R = getValue(FieldName);
522 if (R == 0 || R->getValue() == 0)
523 throw "Record '" + getName() + "' does not have a field named '" +
524 FieldName + "!\n";
525 return R->getValue();
526}
527
528
529/// getValueAsString - This method looks up the specified field and returns its
530/// value as a string, throwing an exception if the field does not exist or if
531/// the value is not a string.
532///
533std::string Record::getValueAsString(const std::string &FieldName) const {
534 const RecordVal *R = getValue(FieldName);
535 if (R == 0 || R->getValue() == 0)
536 throw "Record '" + getName() + "' does not have a field named '" +
537 FieldName + "!\n";
538
539 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
540 return SI->getValue();
541 throw "Record '" + getName() + "', field '" + FieldName +
542 "' does not have a string initializer!";
543}
544
545/// getValueAsBitsInit - This method looks up the specified field and returns
546/// its value as a BitsInit, throwing an exception if the field does not exist
547/// or if the value is not the right type.
548///
549BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
550 const RecordVal *R = getValue(FieldName);
551 if (R == 0 || R->getValue() == 0)
552 throw "Record '" + getName() + "' does not have a field named '" +
553 FieldName + "!\n";
554
555 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
556 return BI;
557 throw "Record '" + getName() + "', field '" + FieldName +
558 "' does not have a BitsInit initializer!";
559}
560
561/// getValueAsListInit - This method looks up the specified field and returns
562/// its value as a ListInit, throwing an exception if the field does not exist
563/// or if the value is not the right type.
564///
565ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
566 const RecordVal *R = getValue(FieldName);
567 if (R == 0 || R->getValue() == 0)
568 throw "Record '" + getName() + "' does not have a field named '" +
569 FieldName + "!\n";
570
571 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
572 return LI;
573 throw "Record '" + getName() + "', field '" + FieldName +
574 "' does not have a list initializer!";
575}
576
577/// getValueAsInt - This method looks up the specified field and returns its
578/// value as an int, throwing an exception if the field does not exist or if
579/// the value is not the right type.
580///
581int Record::getValueAsInt(const std::string &FieldName) const {
582 const RecordVal *R = getValue(FieldName);
583 if (R == 0 || R->getValue() == 0)
584 throw "Record '" + getName() + "' does not have a field named '" +
585 FieldName + "!\n";
586
587 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
588 return II->getValue();
589 throw "Record '" + getName() + "', field '" + FieldName +
590 "' does not have a list initializer!";
591}
592
593/// getValueAsDef - This method looks up the specified field and returns its
594/// value as a Record, throwing an exception if the field does not exist or if
595/// the value is not the right type.
596///
597Record *Record::getValueAsDef(const std::string &FieldName) const {
598 const RecordVal *R = getValue(FieldName);
599 if (R == 0 || R->getValue() == 0)
600 throw "Record '" + getName() + "' does not have a field named '" +
601 FieldName + "!\n";
602
603 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
604 return DI->getDef();
605 throw "Record '" + getName() + "', field '" + FieldName +
606 "' does not have a list initializer!";
607}
608
609/// getValueAsBit - This method looks up the specified field and returns its
610/// value as a bit, throwing an exception if the field does not exist or if
611/// the value is not the right type.
612///
613bool Record::getValueAsBit(const std::string &FieldName) const {
614 const RecordVal *R = getValue(FieldName);
615 if (R == 0 || R->getValue() == 0)
616 throw "Record '" + getName() + "' does not have a field named '" +
617 FieldName + "!\n";
618
619 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
620 return BI->getValue();
621 throw "Record '" + getName() + "', field '" + FieldName +
622 "' does not have a bit initializer!";
623}
624
625/// getValueAsDag - This method looks up the specified field and returns its
626/// value as an Dag, throwing an exception if the field does not exist or if
627/// the value is not the right type.
628///
629DagInit *Record::getValueAsDag(const std::string &FieldName) const {
630 const RecordVal *R = getValue(FieldName);
631 if (R == 0 || R->getValue() == 0)
632 throw "Record '" + getName() + "' does not have a field named '" +
633 FieldName + "!\n";
634
635 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
636 return DI;
637 throw "Record '" + getName() + "', field '" + FieldName +
638 "' does not have a dag initializer!";
639}
640
641
642void RecordKeeper::dump() const { std::cerr << *this; }
643
644std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
645 OS << "------------- Classes -----------------\n";
646 const std::map<std::string, Record*> &Classes = RK.getClasses();
647 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
648 E = Classes.end(); I != E; ++I)
649 OS << "class " << *I->second;
650
651 OS << "------------- Defs -----------------\n";
652 const std::map<std::string, Record*> &Defs = RK.getDefs();
653 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
654 E = Defs.end(); I != E; ++I)
655 OS << "def " << *I->second;
656 return OS;
657}
658
659
660/// getAllDerivedDefinitions - This method returns all concrete definitions
661/// that derive from the specified class name. If a class with the specified
662/// name does not exist, an error is printed and true is returned.
663std::vector<Record*>
664RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
665 Record *Class = Records.getClass(ClassName);
666 if (!Class)
667 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
668
669 std::vector<Record*> Defs;
670 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
671 E = getDefs().end(); I != E; ++I)
672 if (I->second->isSubClassOf(Class))
673 Defs.push_back(I->second);
674
675 return Defs;
676}