//===- Record.cpp - Record implementation ---------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
// Type implementations
//===----------------------------------------------------------------------===//
+namespace llvm {
+
void RecTy::dump() const { print(std::cerr); }
Init *BitRecTy::convertValue(BitsInit *BI) {
return BI->getBit(0);
}
+bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const {
+ return RHS->getNumBits() == 1;
+}
+
Init *BitRecTy::convertValue(IntInit *II) {
int Val = II->getValue();
if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit!
//
Init *BitsRecTy::convertValue(IntInit *II) {
int Value = II->getValue();
+ // Make sure this bitfield is large enough to hold the integer value...
+ if (Value >= 0) {
+ if (Value & ~((1 << Size)-1))
+ return 0;
+ } else {
+ if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0))
+ return 0;
+ }
BitsInit *Ret = new BitsInit(Size);
for (unsigned i = 0; i != Size; ++i)
Ret->setBit(i, new BitInit(Value & (1 << i)));
+
return Ret;
}
return 0;
}
+Init *IntRecTy::convertValue(BitInit *BI) {
+ return new IntInit(BI->getValue());
+}
+
Init *IntRecTy::convertValue(BitsInit *BI) {
int Result = 0;
for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
}
Init *IntRecTy::convertValue(TypedInit *TI) {
- if (dynamic_cast<IntRecTy*>(TI->getType()))
+ if (TI->getType()->typeIsConvertibleTo(this))
return TI; // Accept variable if already of the right type!
return 0;
}
}
void ListRecTy::print(std::ostream &OS) const {
- OS << "list<" << Class->getName() << ">";
+ OS << "list<" << *Ty << ">";
}
Init *ListRecTy::convertValue(ListInit *LI) {
+ std::vector<Init*> Elements;
+
// Verify that all of the elements of the list are subclasses of the
- // appopriate class!
+ // appropriate class!
for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
- if (!LI->getElement(i)->isSubClassOf(Class))
+ if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty))
+ Elements.push_back(CI);
+ else
return 0;
- return LI;
+
+ return new ListInit(Elements);
}
Init *ListRecTy::convertValue(TypedInit *TI) {
// Ensure that TI is compatible with our class.
if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
- if (LRT->getElementClass() == getElementClass())
+ if (LRT->getElementType()->typeIsConvertibleTo(getElementType()))
return TI;
return 0;
}
+Init *CodeRecTy::convertValue(TypedInit *TI) {
+ if (TI->getType()->typeIsConvertibleTo(this))
+ return TI;
+ return 0;
+}
+
+Init *DagRecTy::convertValue(TypedInit *TI) {
+ if (TI->getType()->typeIsConvertibleTo(this))
+ return TI;
+ return 0;
+}
+
+
void RecordRecTy::print(std::ostream &OS) const {
OS << Rec->getName();
}
return 0;
}
+bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
+ return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
+}
+
+
//===----------------------------------------------------------------------===//
// Initializer implementations
//===----------------------------------------------------------------------===//
void ListInit::print(std::ostream &OS) const {
OS << "[";
- for (unsigned i = 0, e = Records.size(); i != e; ++i) {
+ for (unsigned i = 0, e = Values.size(); i != e; ++i) {
if (i) OS << ", ";
- OS << Records[i]->getName();
+ OS << *Values[i];
}
OS << "]";
}
Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
- if (const RecordVal *RV = R.getValue(VarName))
- if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
+ if (const RecordVal *RV = R.getValue(VarName)) {
+ Init *TheInit = RV->getValue();
+ assert(TheInit != this && "Infinite loop detected!");
+ if (Init *I = TheInit->getFieldInit(R, FieldName))
return I;
else
- return (Init*)this;
+ return 0;
+ }
return 0;
}
Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
Init *BitsVal = Rec->getFieldInit(R, FieldName);
- assert(BitsVal && "No initializer found!");
-
- if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
- assert(Bit < BI->getNumBits() && "Bit reference out of range!");
- Init *B = BI->getBit(Bit);
-
- if (dynamic_cast<BitInit*>(B)) // If the bit is set...
- return B; // Replace the VarBitInit with it.
+ if (BitsVal)
+ if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
+ assert(Bit < BI->getNumBits() && "Bit reference out of range!");
+ Init *B = BI->getBit(Bit);
+
+ if (dynamic_cast<BitInit*>(B)) // If the bit is set...
+ return B; // Replace the VarBitInit with it.
+ }
+ return this;
+}
+
+Init *FieldInit::resolveReferences(Record &R) {
+ Init *BitsVal = Rec->getFieldInit(R, FieldName);
+ if (BitsVal) {
+ Init *BVR = BitsVal->resolveReferences(R);
+ return BVR->isComplete() ? BVR : this;
}
return this;
}
+void DagInit::print(std::ostream &OS) const {
+ OS << "(" << NodeTypeDef->getName();
+ if (Args.size()) {
+ OS << " " << *Args[0];
+ if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0];
+ for (unsigned i = 1, e = Args.size(); i != e; ++i) {
+ OS << ", " << *Args[i];
+ if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i];
+ }
+ }
+ OS << ")";
+}
+
+
//===----------------------------------------------------------------------===//
// Other implementations
//===----------------------------------------------------------------------===//
return OS << "}\n";
}
+/// getValueInit - Return the initializer for a value with the specified name,
+/// or throw an exception if the field does not exist.
+///
+Init *Record::getValueInit(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+ return R->getValue();
+}
+
+
+/// getValueAsString - This method looks up the specified field and returns its
+/// value as a string, throwing an exception if the field does not exist or if
+/// the value is not a string.
+///
+std::string Record::getValueAsString(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+
+ if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
+ return SI->getValue();
+ throw "Record '" + getName() + "', field '" + FieldName +
+ "' does not have a string initializer!";
+}
+
+/// getValueAsBitsInit - This method looks up the specified field and returns
+/// its value as a BitsInit, throwing an exception if the field does not exist
+/// or if the value is not the right type.
+///
+BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+
+ if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
+ return BI;
+ throw "Record '" + getName() + "', field '" + FieldName +
+ "' does not have a BitsInit initializer!";
+}
+
+/// getValueAsListInit - This method looks up the specified field and returns
+/// its value as a ListInit, throwing an exception if the field does not exist
+/// or if the value is not the right type.
+///
+ListInit *Record::getValueAsListInit(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+
+ if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue()))
+ return LI;
+ throw "Record '" + getName() + "', field '" + FieldName +
+ "' does not have a list initializer!";
+}
+
+/// getValueAsInt - This method looks up the specified field and returns its
+/// value as an int, throwing an exception if the field does not exist or if
+/// the value is not the right type.
+///
+int Record::getValueAsInt(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+
+ if (IntInit *II = dynamic_cast<IntInit*>(R->getValue()))
+ return II->getValue();
+ throw "Record '" + getName() + "', field '" + FieldName +
+ "' does not have a list initializer!";
+}
+
+/// getValueAsDef - This method looks up the specified field and returns its
+/// value as a Record, throwing an exception if the field does not exist or if
+/// the value is not the right type.
+///
+Record *Record::getValueAsDef(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+
+ if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue()))
+ return DI->getDef();
+ throw "Record '" + getName() + "', field '" + FieldName +
+ "' does not have a list initializer!";
+}
+
+/// getValueAsBit - This method looks up the specified field and returns its
+/// value as a bit, throwing an exception if the field does not exist or if
+/// the value is not the right type.
+///
+bool Record::getValueAsBit(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+
+ if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue()))
+ return BI->getValue();
+ throw "Record '" + getName() + "', field '" + FieldName +
+ "' does not have a bit initializer!";
+}
+
+/// getValueAsDag - This method looks up the specified field and returns its
+/// value as an Dag, throwing an exception if the field does not exist or if
+/// the value is not the right type.
+///
+DagInit *Record::getValueAsDag(const std::string &FieldName) const {
+ const RecordVal *R = getValue(FieldName);
+ if (R == 0 || R->getValue() == 0)
+ throw "Record '" + getName() + "' does not have a field named '" +
+ FieldName + "!\n";
+
+ if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue()))
+ return DI;
+ throw "Record '" + getName() + "', field '" + FieldName +
+ "' does not have a dag initializer!";
+}
+
+
void RecordKeeper::dump() const { std::cerr << *this; }
std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
OS << "def " << *I->second;
return OS;
}
+
+
+/// getAllDerivedDefinitions - This method returns all concrete definitions
+/// that derive from the specified class name. If a class with the specified
+/// name does not exist, an error is printed and true is returned.
+std::vector<Record*>
+RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
+ Record *Class = Records.getClass(ClassName);
+ if (!Class)
+ throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
+
+ std::vector<Record*> Defs;
+ for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
+ E = getDefs().end(); I != E; ++I)
+ if (I->second->isSubClassOf(Class))
+ Defs.push_back(I->second);
+
+ return Defs;
+}
+
+} // End llvm namespace