implement new method
[oota-llvm.git] / utils / TableGen / Record.cpp
index 5931cb8db261c61bc4619e953604ebf6b53b5608..1a60ac489e082171de9f6253ddbb2726daac144d 100644 (file)
@@ -1,4 +1,11 @@
 //===- 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.
+// 
+//===----------------------------------------------------------------------===//
 //
 //
 //===----------------------------------------------------------------------===//
@@ -9,6 +16,8 @@
 //    Type implementations
 //===----------------------------------------------------------------------===//
 
+namespace llvm {
+
 void RecTy::dump() const { print(std::cerr); }
 
 Init *BitRecTy::convertValue(BitsInit *BI) {
@@ -16,10 +25,13 @@ 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!
-  delete II;
   
   return new BitInit(Val != 0); 
 }
@@ -50,11 +62,19 @@ Init *BitsRecTy::convertValue(BitInit *UI) {
 //
 Init *BitsRecTy::convertValue(IntInit *II) {
   int Value = II->getValue();
-  delete II;
+  // 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;
 }
 
@@ -82,6 +102,10 @@ Init *BitsRecTy::convertValue(TypedInit *VI) {
   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) 
@@ -94,7 +118,7 @@ Init *IntRecTy::convertValue(BitsInit *BI) {
 }
 
 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;
 }
@@ -106,18 +130,44 @@ Init *StringRecTy::convertValue(TypedInit *TI) {
 }
 
 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->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();
 }
@@ -129,6 +179,20 @@ 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;
+}
+
+bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const {
+  return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec);
+}
+
+
 //===----------------------------------------------------------------------===//
 //    Initializer implementations
 //===----------------------------------------------------------------------===//
@@ -155,7 +219,10 @@ void BitsInit::print(std::ostream &OS) const {
   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 << " }";
 }
@@ -205,18 +272,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)
@@ -240,9 +312,9 @@ Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
 
 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 << "]";
 }
@@ -289,15 +361,29 @@ RecTy *VarInit::getFieldType(const std::string &FieldName) const {
 
 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;
 }
 
-
+/// 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());
@@ -339,19 +425,41 @@ Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
 
 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
 //===----------------------------------------------------------------------===//
@@ -418,6 +526,131 @@ std::ostream &operator<<(std::ostream &OS, const Record &R) {
   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) {
@@ -434,3 +667,24 @@ 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