Add new getValueAsBitsInit 'high-level' method
[oota-llvm.git] / support / tools / TableGen / Record.cpp
index 64711bfaa4c6fd17e8e87640bc4b391dab749ad1..acb61ec18525386dcefd79a9f3d7e1245cd56d4e 100644 (file)
@@ -19,12 +19,11 @@ Init *BitRecTy::convertValue(BitsInit *BI) {
 Init *BitRecTy::convertValue(IntInit *II) {
   int Val = II->getValue();
   if (Val != 0 && Val != 1) return 0;  // Only accept 0 or 1 for a bit!
-  delete II;
   
   return new BitInit(Val != 0); 
 }
 
-Init *BitRecTy::convertValue(VarInit *VI) {
+Init *BitRecTy::convertValue(TypedInit *VI) {
   if (dynamic_cast<BitRecTy*>(VI->getType()))
     return VI;  // Accept variable if it is already of bit type!
   return 0;
@@ -50,7 +49,6 @@ Init *BitsRecTy::convertValue(BitInit *UI) {
 //
 Init *BitsRecTy::convertValue(IntInit *II) {
   int Value = II->getValue();
-  delete II;
 
   BitsInit *Ret = new BitsInit(Size);
   for (unsigned i = 0; i != Size; ++i)
@@ -65,7 +63,7 @@ Init *BitsRecTy::convertValue(BitsInit *BI) {
   return 0;
 }
 
-Init *BitsRecTy::convertValue(VarInit *VI) {
+Init *BitsRecTy::convertValue(TypedInit *VI) {
   if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType()))
     if (BRT->Size == Size) {
       BitsInit *Ret = new BitsInit(Size);
@@ -82,11 +80,6 @@ Init *BitsRecTy::convertValue(VarInit *VI) {
   return 0;
 }
 
-Init *BitsRecTy::convertValue(FieldInit *VI) {
-  return 0;
-}
-
-
 Init *IntRecTy::convertValue(BitsInit *BI) {
   int Result = 0;
   for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) 
@@ -98,15 +91,15 @@ Init *IntRecTy::convertValue(BitsInit *BI) {
   return new IntInit(Result);
 }
 
-Init *IntRecTy::convertValue(VarInit *VI) {
-  if (dynamic_cast<IntRecTy*>(VI->getType()))
-    return VI;  // Accept variable if already of the right type!
+Init *IntRecTy::convertValue(TypedInit *TI) {
+  if (dynamic_cast<IntRecTy*>(TI->getType()))
+    return TI;  // Accept variable if already of the right type!
   return 0;
 }
 
-Init *StringRecTy::convertValue(VarInit *VI) {
-  if (dynamic_cast<StringRecTy*>(VI->getType()))
-    return VI;  // Accept variable if already of the right type!
+Init *StringRecTy::convertValue(TypedInit *TI) {
+  if (dynamic_cast<StringRecTy*>(TI->getType()))
+    return TI;  // Accept variable if already of the right type!
   return 0;
 }
 
@@ -123,6 +116,14 @@ Init *ListRecTy::convertValue(ListInit *LI) {
   return LI;
 }
 
+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())
+      return TI;
+  return 0;
+}
+
 void RecordRecTy::print(std::ostream &OS) const {
   OS << Rec->getName();
 }
@@ -134,6 +135,15 @@ Init *RecordRecTy::convertValue(DefInit *DI) {
   return DI;
 }
 
+Init *RecordRecTy::convertValue(TypedInit *TI) {
+  // Ensure that TI is compatible with Rec.
+  if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
+    if (RRT->getRecord()->isSubClassOf(getRecord()) ||
+        RRT->getRecord() == getRecord())
+      return TI;
+  return 0;
+}
+
 //===----------------------------------------------------------------------===//
 //    Initializer implementations
 //===----------------------------------------------------------------------===//
@@ -154,13 +164,16 @@ Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
 
 void BitsInit::print(std::ostream &OS) const {
   //if (!printInHex(OS)) return;
-  if (!printAsVariable(OS)) return;
-  if (!printAsUnset(OS)) return;
+  //if (!printAsVariable(OS)) return;
+  //if (!printAsUnset(OS)) return;
 
   OS << "{ ";
   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
     if (i) OS << ", ";
-    getBit(e-i-1)->print(OS);
+    if (Init *Bit = getBit(e-i-1))
+      Bit->print(OS);
+    else
+      OS << "*";
   }
   OS << " }";
 }
@@ -210,18 +223,23 @@ bool BitsInit::printAsUnset(std::ostream &OS) const {
   return false;
 }
 
+// resolveReferences - If there are any field references that refer to fields
+// that have been filled in, we can propagate the values now.
+//
 Init *BitsInit::resolveReferences(Record &R) {
   bool Changed = false;
   BitsInit *New = new BitsInit(getNumBits());
 
   for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
     Init *B;
-    New->setBit(i, getBit(i));
+    Init *CurBit = getBit(i);
+
     do {
-      B = New->getBit(i);
-      New->setBit(i, B->resolveReferences(R));
-      Changed |= B != New->getBit(i);
-    } while (B != New->getBit(i));
+      B = CurBit;
+      CurBit = CurBit->resolveReferences(R);
+      Changed |= B != CurBit;
+    } while (B != CurBit);
+    New->setBit(i, CurBit);
   }
 
   if (Changed)
@@ -268,13 +286,6 @@ Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
   return BI;
 }
 
-RecTy *VarInit::getFieldType(const std::string &FieldName) const {
-  if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
-    if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
-      return RV->getType();
-  return 0;
-}
-
 Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
   if (R.isTemplateArg(getName()))
     return this;
@@ -292,16 +303,54 @@ Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
   return this;
 }
 
+RecTy *VarInit::getFieldType(const std::string &FieldName) const {
+  if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType()))
+    if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName))
+      return RV->getType();
+  return 0;
+}
 
+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))
+        return I;
+      else
+        return (Init*)this;
+  return 0;
+}
+
+/// resolveReferences - This method is used by classes that refer to other
+/// variables which may not be defined at the time they expression is formed.
+/// If a value is set for the variable later, this method will be called on
+/// users of the value to allow the value to propagate out.
+///
+Init *VarInit::resolveReferences(Record &R) {
+  if (RecordVal *Val = R.getValue(VarName))
+    if (!dynamic_cast<UnsetInit*>(Val->getValue()))
+      return Val->getValue();
+  return this;
+}
+  
 
 Init *VarBitInit::resolveReferences(Record &R) {
   Init *I = getVariable()->resolveBitReference(R, getBitNum());
-
   if (I != getVariable())
     return I;
   return this;
 }
 
+RecTy *DefInit::getFieldType(const std::string &FieldName) const {
+  if (const RecordVal *RV = Def->getValue(FieldName))
+    return RV->getType();
+  return 0;
+}
+
+Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const {
+  return Def->getValue(FieldName)->getValue();
+}
+
+
 void DefInit::print(std::ostream &OS) const {
   OS << Def->getName();
 }
@@ -323,7 +372,16 @@ Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
 }
 
 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
-  // Can never be resolved yet.
+  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.
+  }
   return this;
 }
 
@@ -394,6 +452,40 @@ std::ostream &operator<<(std::ostream &OS, const Record &R) {
   return OS << "}\n";
 }
 
+/// 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 '" + R->getName() + "' does not have a field named '" +
+          FieldName + "!\n";
+
+  if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
+    return SI->getValue();
+  throw "Record '" + R->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 '" + R->getName() + "' does not have a field named '" +
+          FieldName + "!\n";
+
+  if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
+    return BI;
+  throw "Record '" + R->getName() + "', field '" + FieldName +
+        "' does not have a BitsInit initializer!";
+}
+
+
+
 void RecordKeeper::dump() const { std::cerr << *this; }
 
 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
@@ -410,3 +502,22 @@ 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;
+}