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;
//
Init *BitsRecTy::convertValue(IntInit *II) {
int Value = II->getValue();
- delete II;
BitsInit *Ret = new BitsInit(Size);
for (unsigned i = 0; i != Size; ++i)
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);
return 0;
}
-
Init *IntRecTy::convertValue(BitsInit *BI) {
int Result = 0;
for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
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;
}
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 << " }";
}
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!
return true;
}
- OS << Var->getName();
+ Var->print(OS);
return false;
}
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());
}
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();
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
//===----------------------------------------------------------------------===//