1 //===- Record.cpp - Record implementation ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
15 //===----------------------------------------------------------------------===//
16 // Type implementations
17 //===----------------------------------------------------------------------===//
21 void RecTy::dump() const { print(std::cerr); }
23 Init *BitRecTy::convertValue(BitsInit *BI) {
24 if (BI->getNumBits() != 1) return 0; // Only accept if just one bit!
28 bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
29 return RHS->getNumBits() == 1;
32 Init *BitRecTy::convertValue(IntInit *II) {
33 int Val = II->getValue();
34 if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
36 return new BitInit(Val != 0);
39 Init *BitRecTy::convertValue(TypedInit *VI) {
40 if (dynamic_cast<BitRecTy*>(VI->getType()))
41 return VI; // Accept variable if it is already of bit type!
45 Init *BitsRecTy::convertValue(UnsetInit *UI) {
46 BitsInit *Ret = new BitsInit(Size);
48 for (unsigned i = 0; i != Size; ++i)
49 Ret->setBit(i, new UnsetInit());
53 Init *BitsRecTy::convertValue(BitInit *UI) {
54 if (Size != 1) return 0; // Can only convert single bit...
55 BitsInit *Ret = new BitsInit(1);
60 // convertValue from Int initializer to bits type: Split the integer up into the
61 // appropriate bits...
63 Init *BitsRecTy::convertValue(IntInit *II) {
64 int64_t Value = II->getValue();
65 // Make sure this bitfield is large enough to hold the integer value...
67 if (Value & ~((1LL << Size)-1))
70 if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
74 BitsInit *Ret = new BitsInit(Size);
75 for (unsigned i = 0; i != Size; ++i)
76 Ret->setBit(i, new BitInit(Value & (1 << i)));
81 Init *BitsRecTy::convertValue(BitsInit *BI) {
82 // If the number of bits is right, return it. Otherwise we need to expand or
84 if (BI->getNumBits() == Size) return BI;
88 Init *BitsRecTy::convertValue(TypedInit *VI) {
89 if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
90 if (BRT->Size == Size) {
91 BitsInit *Ret = new BitsInit(Size);
92 for (unsigned i = 0; i != Size; ++i)
93 Ret->setBit(i, new VarBitInit(VI, i));
96 if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) {
97 BitsInit *Ret = new BitsInit(1);
105 Init *IntRecTy::convertValue(BitInit *BI) {
106 return new IntInit(BI->getValue());
109 Init *IntRecTy::convertValue(BitsInit *BI) {
111 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
112 if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) {
113 Result |= Bit->getValue() << i;
117 return new IntInit(Result);
120 Init *IntRecTy::convertValue(TypedInit *TI) {
121 if (TI->getType()->typeIsConvertibleTo(this))
122 return TI; // Accept variable if already of the right type!
126 Init *StringRecTy::convertValue(TypedInit *TI) {
127 if (dynamic_cast<StringRecTy*>(TI->getType()))
128 return TI; // Accept variable if already of the right type!
132 void ListRecTy::print(std::ostream &OS) const {
133 OS << "list<" << *Ty << ">";
136 Init *ListRecTy::convertValue(ListInit *LI) {
137 std::vector<Init*> Elements;
139 // Verify that all of the elements of the list are subclasses of the
140 // appropriate class!
141 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
142 if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
143 Elements.push_back(CI);
147 return new ListInit(Elements);
150 Init *ListRecTy::convertValue(TypedInit *TI) {
151 // Ensure that TI is compatible with our class.
152 if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
153 if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
158 Init *CodeRecTy::convertValue(TypedInit *TI) {
159 if (TI->getType()->typeIsConvertibleTo(this))
164 Init *DagRecTy::convertValue(TypedInit *TI) {
165 if (TI->getType()->typeIsConvertibleTo(this))
171 void RecordRecTy::print(std::ostream &OS) const {
172 OS << Rec->getName();
175 Init *RecordRecTy::convertValue(DefInit *DI) {
176 // Ensure that DI is a subclass of Rec.
177 if (!DI->getDef()->isSubClassOf(Rec))
182 Init *RecordRecTy::convertValue(TypedInit *TI) {
183 // Ensure that TI is compatible with Rec.
184 if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
185 if (RRT->getRecord()->isSubClassOf(getRecord()) ||
186 RRT->getRecord() == getRecord())
191 bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
192 return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
196 //===----------------------------------------------------------------------===//
197 // Initializer implementations
198 //===----------------------------------------------------------------------===//
200 void Init::dump() const { return print(std::cerr); }
202 Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
203 BitsInit *BI = new BitsInit(Bits.size());
204 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
205 if (Bits[i] >= getNumBits()) {
209 BI->setBit(i, getBit(Bits[i]));
214 void BitsInit::print(std::ostream &OS) const {
215 //if (!printInHex(OS)) return;
216 //if (!printAsVariable(OS)) return;
217 //if (!printAsUnset(OS)) return;
220 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
222 if (Init *Bit = getBit(e-i-1))
230 bool BitsInit::printInHex(std::ostream &OS) const {
231 // First, attempt to convert the value into an integer value...
233 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
234 if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) {
235 Result |= Bit->getValue() << i;
240 OS << "0x" << std::hex << Result << std::dec;
244 bool BitsInit::printAsVariable(std::ostream &OS) const {
245 // Get the variable that we may be set equal to...
246 assert(getNumBits() != 0);
247 VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
248 if (FirstBit == 0) return true;
249 TypedInit *Var = FirstBit->getVariable();
251 // Check to make sure the types are compatible.
252 BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
253 if (Ty == 0) return true;
254 if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
256 // Check to make sure all bits are referring to the right bits in the variable
257 for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
258 VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i));
259 if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i)
267 bool BitsInit::printAsUnset(std::ostream &OS) const {
268 for (unsigned i = 0, e = getNumBits(); i != e; ++i)
269 if (!dynamic_cast<UnsetInit*>(getBit(i)))
275 // resolveReferences - If there are any field references that refer to fields
276 // that have been filled in, we can propagate the values now.
278 Init *BitsInit::resolveReferences(Record &R) {
279 bool Changed = false;
280 BitsInit *New = new BitsInit(getNumBits());
282 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
284 Init *CurBit = getBit(i);
288 CurBit = CurBit->resolveReferences(R);
289 Changed |= B != CurBit;
290 } while (B != CurBit);
291 New->setBit(i, CurBit);
300 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
301 BitsInit *BI = new BitsInit(Bits.size());
303 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
308 BI->setBit(i, new BitInit(Value & (1 << Bits[i])));
313 void ListInit::print(std::ostream &OS) const {
315 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
322 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
323 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
324 if (T == 0) return 0; // Cannot subscript a non-bits variable...
325 unsigned NumBits = T->getNumBits();
327 BitsInit *BI = new BitsInit(Bits.size());
328 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
329 if (Bits[i] >= NumBits) {
333 BI->setBit(i, new VarBitInit(this, Bits[i]));
338 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
339 if (R.isTemplateArg(getName()))
342 RecordVal *RV = R.getValue(getName());
343 assert(RV && "Reference to a non-existant variable?");
344 assert(dynamic_cast<BitsInit*>(RV->getValue()));
345 BitsInit *BI = (BitsInit*)RV->getValue();
347 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
348 Init *B = BI->getBit(Bit);
350 if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set...
351 return B; // Replace the VarBitInit with it.
355 RecTy *VarInit::getFieldType(const std::string &FieldName) const {
356 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
357 if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
358 return RV->getType();
362 Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
363 if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
364 if (const RecordVal *RV = R.getValue(VarName)) {
365 Init *TheInit = RV->getValue();
366 assert(TheInit != this && "Infinite loop detected!");
367 if (Init *I = TheInit->getFieldInit(R, FieldName))
375 /// resolveReferences - This method is used by classes that refer to other
376 /// variables which may not be defined at the time they expression is formed.
377 /// If a value is set for the variable later, this method will be called on
378 /// users of the value to allow the value to propagate out.
380 Init *VarInit::resolveReferences(Record &R) {
381 if (RecordVal *Val = R.getValue(VarName))
382 if (!dynamic_cast<UnsetInit*>(Val->getValue()))
383 return Val->getValue();
388 Init *VarBitInit::resolveReferences(Record &R) {
389 Init *I = getVariable()->resolveBitReference(R, getBitNum());
390 if (I != getVariable())
395 RecTy *DefInit::getFieldType(const std::string &FieldName) const {
396 if (const RecordVal *RV = Def->getValue(FieldName))
397 return RV->getType();
401 Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
402 return Def->getValue(FieldName)->getValue();
406 void DefInit::print(std::ostream &OS) const {
407 OS << Def->getName();
410 Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
411 BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
412 if (T == 0) return 0; // Cannot subscript a non-bits field...
413 unsigned NumBits = T->getNumBits();
415 BitsInit *BI = new BitsInit(Bits.size());
416 for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
417 if (Bits[i] >= NumBits) {
421 BI->setBit(i, new VarBitInit(this, Bits[i]));
426 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
427 Init *BitsVal = Rec->getFieldInit(R, FieldName);
429 if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
430 assert(Bit < BI->getNumBits() && "Bit reference out of range!");
431 Init *B = BI->getBit(Bit);
433 if (dynamic_cast<BitInit*>(B)) // If the bit is set...
434 return B; // Replace the VarBitInit with it.
439 Init *FieldInit::resolveReferences(Record &R) {
440 Init *BitsVal = Rec->getFieldInit(R, FieldName);
442 Init *BVR = BitsVal->resolveReferences(R);
443 return BVR->isComplete() ? BVR : this;
449 void DagInit::print(std::ostream &OS) const {
450 OS << "(" << NodeTypeDef->getName();
452 OS << " " << *Args[0];
453 if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
454 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
455 OS << ", " << *Args[i];
456 if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
463 //===----------------------------------------------------------------------===//
464 // Other implementations
465 //===----------------------------------------------------------------------===//
467 RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P)
468 : Name(N), Ty(T), Prefix(P) {
469 Value = Ty->convertValue(new UnsetInit());
470 assert(Value && "Cannot create unset value for current type!");
473 void RecordVal::dump() const { std::cerr << *this; }
475 void RecordVal::print(std::ostream &OS, bool PrintSem) const {
476 if (getPrefix()) OS << "field ";
477 OS << *getType() << " " << getName();
479 OS << " = " << *getValue();
481 if (PrintSem) OS << ";\n";
484 // resolveReferences - If there are any field references that refer to fields
485 // that have been filled in, we can propagate the values now.
487 void Record::resolveReferences() {
488 for (unsigned i = 0, e = Values.size(); i != e; ++i)
489 Values[i].setValue(Values[i].getValue()->resolveReferences(*this));
492 void Record::dump() const { std::cerr << *this; }
494 std::ostream &operator<<(std::ostream &OS, const Record &R) {
497 const std::vector<std::string> &TArgs = R.getTemplateArgs();
498 if (!TArgs.empty()) {
500 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
502 const RecordVal *RV = R.getValue(TArgs[i]);
503 assert(RV && "Template argument record not found??");
504 RV->print(OS, false);
510 const std::vector<Record*> &SC = R.getSuperClasses();
513 for (unsigned i = 0, e = SC.size(); i != e; ++i)
514 OS << " " << SC[i]->getName();
518 const std::vector<RecordVal> &Vals = R.getValues();
519 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
520 if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
522 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
523 if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName()))
529 /// getValueInit - Return the initializer for a value with the specified name,
530 /// or throw an exception if the field does not exist.
532 Init *Record::getValueInit(const std::string &FieldName) const {
533 const RecordVal *R = getValue(FieldName);
534 if (R == 0 || R->getValue() == 0)
535 throw "Record '" + getName() + "' does not have a field named '" +
537 return R->getValue();
541 /// getValueAsString - This method looks up the specified field and returns its
542 /// value as a string, throwing an exception if the field does not exist or if
543 /// the value is not a string.
545 std::string Record::getValueAsString(const std::string &FieldName) const {
546 const RecordVal *R = getValue(FieldName);
547 if (R == 0 || R->getValue() == 0)
548 throw "Record '" + getName() + "' does not have a field named '" +
551 if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
552 return SI->getValue();
553 throw "Record '" + getName() + "', field '" + FieldName +
554 "' does not have a string initializer!";
557 /// getValueAsBitsInit - This method looks up the specified field and returns
558 /// its value as a BitsInit, throwing an exception if the field does not exist
559 /// or if the value is not the right type.
561 BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
562 const RecordVal *R = getValue(FieldName);
563 if (R == 0 || R->getValue() == 0)
564 throw "Record '" + getName() + "' does not have a field named '" +
567 if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
569 throw "Record '" + getName() + "', field '" + FieldName +
570 "' does not have a BitsInit initializer!";
573 /// getValueAsListInit - This method looks up the specified field and returns
574 /// its value as a ListInit, throwing an exception if the field does not exist
575 /// or if the value is not the right type.
577 ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
578 const RecordVal *R = getValue(FieldName);
579 if (R == 0 || R->getValue() == 0)
580 throw "Record '" + getName() + "' does not have a field named '" +
583 if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
585 throw "Record '" + getName() + "', field '" + FieldName +
586 "' does not have a list initializer!";
589 /// getValueAsInt - This method looks up the specified field and returns its
590 /// value as an int, throwing an exception if the field does not exist or if
591 /// the value is not the right type.
593 int Record::getValueAsInt(const std::string &FieldName) const {
594 const RecordVal *R = getValue(FieldName);
595 if (R == 0 || R->getValue() == 0)
596 throw "Record '" + getName() + "' does not have a field named '" +
599 if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
600 return II->getValue();
601 throw "Record '" + getName() + "', field '" + FieldName +
602 "' does not have a list initializer!";
605 /// getValueAsDef - This method looks up the specified field and returns its
606 /// value as a Record, throwing an exception if the field does not exist or if
607 /// the value is not the right type.
609 Record *Record::getValueAsDef(const std::string &FieldName) const {
610 const RecordVal *R = getValue(FieldName);
611 if (R == 0 || R->getValue() == 0)
612 throw "Record '" + getName() + "' does not have a field named '" +
615 if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
617 throw "Record '" + getName() + "', field '" + FieldName +
618 "' does not have a list initializer!";
621 /// getValueAsBit - This method looks up the specified field and returns its
622 /// value as a bit, throwing an exception if the field does not exist or if
623 /// the value is not the right type.
625 bool Record::getValueAsBit(const std::string &FieldName) const {
626 const RecordVal *R = getValue(FieldName);
627 if (R == 0 || R->getValue() == 0)
628 throw "Record '" + getName() + "' does not have a field named '" +
631 if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
632 return BI->getValue();
633 throw "Record '" + getName() + "', field '" + FieldName +
634 "' does not have a bit initializer!";
637 /// getValueAsDag - This method looks up the specified field and returns its
638 /// value as an Dag, throwing an exception if the field does not exist or if
639 /// the value is not the right type.
641 DagInit *Record::getValueAsDag(const std::string &FieldName) const {
642 const RecordVal *R = getValue(FieldName);
643 if (R == 0 || R->getValue() == 0)
644 throw "Record '" + getName() + "' does not have a field named '" +
647 if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
649 throw "Record '" + getName() + "', field '" + FieldName +
650 "' does not have a dag initializer!";
654 void RecordKeeper::dump() const { std::cerr << *this; }
656 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
657 OS << "------------- Classes -----------------\n";
658 const std::map<std::string, Record*> &Classes = RK.getClasses();
659 for (std::map<std::string, Record*>::const_iterator I = Classes.begin(),
660 E = Classes.end(); I != E; ++I)
661 OS << "class " << *I->second;
663 OS << "------------- Defs -----------------\n";
664 const std::map<std::string, Record*> &Defs = RK.getDefs();
665 for (std::map<std::string, Record*>::const_iterator I = Defs.begin(),
666 E = Defs.end(); I != E; ++I)
667 OS << "def " << *I->second;
672 /// getAllDerivedDefinitions - This method returns all concrete definitions
673 /// that derive from the specified class name. If a class with the specified
674 /// name does not exist, an error is printed and true is returned.
676 RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
677 Record *Class = Records.getClass(ClassName);
679 throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
681 std::vector<Record*> Defs;
682 for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
683 E = getDefs().end(); I != E; ++I)
684 if (I->second->isSubClassOf(Class))
685 Defs.push_back(I->second);
690 } // End llvm namespace