Remove dead makefile code
[oota-llvm.git] / utils / TableGen / Record.cpp
index 8324bad5c162e42a44427d838cf56986395550fd..469faa9b01bf4d290d1255d3b1869f7973900644 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,7 +80,6 @@ Init *BitsRecTy::convertValue(VarInit *VI) {
   return 0;
 }
 
-
 Init *IntRecTy::convertValue(BitsInit *BI) {
   int Result = 0;
   for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) 
@@ -94,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;
 }
 
@@ -150,13 +147,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 << " }";
 }
@@ -180,10 +180,10 @@ bool BitsInit::printAsVariable(std::ostream &OS) const {
   assert(getNumBits() != 0);
   VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0));
   if (FirstBit == 0) return true;
-  VarInit *Var = FirstBit->getVariable();
+  TypedInit *Var = FirstBit->getVariable();
 
   // Check to make sure the types are compatible.
-  BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(Var->getType());
+  BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType());
   if (Ty == 0) return true;
   if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types!
 
@@ -194,7 +194,7 @@ bool BitsInit::printAsVariable(std::ostream &OS) const {
       return true;
   }
 
-  OS << Var->getName();
+  Var->print(OS);
   return false;
 }
 
@@ -206,6 +206,28 @@ bool BitsInit::printAsUnset(std::ostream &OS) const {
   return false;
 }
 
+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;
+    Init *CurBit = getBit(i);
+
+    do {
+      B = CurBit;
+      CurBit = CurBit->resolveReferences(R);
+      Changed |= B != CurBit;
+    } while (B != CurBit);
+    New->setBit(i, CurBit);
+  }
+
+  if (Changed)
+    return New;
+  delete New;
+  return this;
+}
+
 Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
   BitsInit *BI = new BitsInit(Bits.size());
 
@@ -229,7 +251,7 @@ void ListInit::print(std::ostream &OS) const {
 }
 
 Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
-  BitsRecTy *T = dynamic_cast<BitsRecTy*>(Ty);
+  BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
   if (T == 0) return 0;  // Cannot subscript a non-bits variable...
   unsigned NumBits = T->getNumBits();
 
@@ -244,49 +266,95 @@ Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
   return BI;
 }
 
-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));
-    do {
-      B = New->getBit(i);
-      New->setBit(i, B->resolveReferences(R));
-      Changed |= B != New->getBit(i);
-    } while (B != New->getBit(i));
-  }
-
-  if (Changed)
-    return New;
-  delete New;
-  return this;
-}
-
-
-
-Init *VarBitInit::resolveReferences(Record &R) {
-  if (R.isTemplateArg(getVariable()->getName()))
+Init *VarInit::resolveBitReference(Record &R, unsigned Bit) {
+  if (R.isTemplateArg(getName()))
     return this;
 
-  RecordVal *RV = R.getValue(getVariable()->getName());
+  RecordVal *RV = R.getValue(getName());
   assert(RV && "Reference to a non-existant variable?");
   assert(dynamic_cast<BitsInit*>(RV->getValue()));
   BitsInit *BI = (BitsInit*)RV->getValue();
   
-  assert(getBitNum() < BI->getNumBits() && "Bit reference out of range!");
-  Init *B = BI->getBit(getBitNum());
+  assert(Bit < BI->getNumBits() && "Bit reference out of range!");
+  Init *B = BI->getBit(Bit);
 
   if (!dynamic_cast<UnsetInit*>(B))  // If the bit is not set...
     return B;                        // Replace the VarBitInit with it.
   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;
+}
+
+
+
+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();
 }
 
+Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
+  BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType());
+  if (T == 0) return 0;  // Cannot subscript a non-bits field...
+  unsigned NumBits = T->getNumBits();
+
+  BitsInit *BI = new BitsInit(Bits.size());
+  for (unsigned i = 0, e = Bits.size(); i != e; ++i) {
+    if (Bits[i] >= NumBits) {
+      delete BI;
+      return 0;
+    }
+    BI->setBit(i, new VarBitInit(this, Bits[i]));
+  }
+  return BI;
+}
+
+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.
+  }
+  return this;
+}
+
+
 //===----------------------------------------------------------------------===//
 //    Other implementations
 //===----------------------------------------------------------------------===//