Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1 | //===- Record.cpp - Record implementation ---------------------------------===// |
John Criswell | d303203 | 2003-10-20 20:20:30 +0000 | [diff] [blame^] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "Record.h" |
| 14 | |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | // Type implementations |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | void RecTy::dump() const { print(std::cerr); } |
| 20 | |
| 21 | Init *BitRecTy::convertValue(BitsInit *BI) { |
| 22 | if (BI->getNumBits() != 1) return 0; // Only accept if just one bit! |
| 23 | return BI->getBit(0); |
| 24 | } |
| 25 | |
| 26 | bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const { |
| 27 | return RHS->getNumBits() == 1; |
| 28 | } |
| 29 | |
| 30 | Init *BitRecTy::convertValue(IntInit *II) { |
| 31 | int Val = II->getValue(); |
| 32 | if (Val != 0 && Val != 1) return 0; // Only accept 0 or 1 for a bit! |
| 33 | |
| 34 | return new BitInit(Val != 0); |
| 35 | } |
| 36 | |
| 37 | Init *BitRecTy::convertValue(TypedInit *VI) { |
| 38 | if (dynamic_cast<BitRecTy*>(VI->getType())) |
| 39 | return VI; // Accept variable if it is already of bit type! |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | Init *BitsRecTy::convertValue(UnsetInit *UI) { |
| 44 | BitsInit *Ret = new BitsInit(Size); |
| 45 | |
| 46 | for (unsigned i = 0; i != Size; ++i) |
| 47 | Ret->setBit(i, new UnsetInit()); |
| 48 | return Ret; |
| 49 | } |
| 50 | |
| 51 | Init *BitsRecTy::convertValue(BitInit *UI) { |
| 52 | if (Size != 1) return 0; // Can only convert single bit... |
| 53 | BitsInit *Ret = new BitsInit(1); |
| 54 | Ret->setBit(0, UI); |
| 55 | return Ret; |
| 56 | } |
| 57 | |
| 58 | // convertValue from Int initializer to bits type: Split the integer up into the |
| 59 | // appropriate bits... |
| 60 | // |
| 61 | Init *BitsRecTy::convertValue(IntInit *II) { |
| 62 | int Value = II->getValue(); |
| 63 | // Make sure this bitfield is large enough to hold the integer value... |
| 64 | if (Value >= 0) { |
| 65 | if (Value & ~((1 << Size)-1)) |
| 66 | return 0; |
| 67 | } else { |
| 68 | if ((Value >> Size) != -1 || ((Value & (1 << Size-1)) == 0)) |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | BitsInit *Ret = new BitsInit(Size); |
| 73 | for (unsigned i = 0; i != Size; ++i) |
| 74 | Ret->setBit(i, new BitInit(Value & (1 << i))); |
| 75 | |
| 76 | return Ret; |
| 77 | } |
| 78 | |
| 79 | Init *BitsRecTy::convertValue(BitsInit *BI) { |
| 80 | // If the number of bits is right, return it. Otherwise we need to expand or |
| 81 | // truncate... |
| 82 | if (BI->getNumBits() == Size) return BI; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | Init *BitsRecTy::convertValue(TypedInit *VI) { |
| 87 | if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType())) |
| 88 | if (BRT->Size == Size) { |
| 89 | BitsInit *Ret = new BitsInit(Size); |
| 90 | for (unsigned i = 0; i != Size; ++i) |
| 91 | Ret->setBit(i, new VarBitInit(VI, i)); |
| 92 | return Ret; |
| 93 | } |
| 94 | if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) { |
| 95 | BitsInit *Ret = new BitsInit(1); |
| 96 | Ret->setBit(0, VI); |
| 97 | return Ret; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | Init *IntRecTy::convertValue(BitInit *BI) { |
| 104 | return new IntInit(BI->getValue()); |
| 105 | } |
| 106 | |
| 107 | Init *IntRecTy::convertValue(BitsInit *BI) { |
| 108 | int Result = 0; |
| 109 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) |
| 110 | if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) { |
| 111 | Result |= Bit->getValue() << i; |
| 112 | } else { |
| 113 | return 0; |
| 114 | } |
| 115 | return new IntInit(Result); |
| 116 | } |
| 117 | |
| 118 | Init *IntRecTy::convertValue(TypedInit *TI) { |
| 119 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 120 | return TI; // Accept variable if already of the right type! |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | Init *StringRecTy::convertValue(TypedInit *TI) { |
| 125 | if (dynamic_cast<StringRecTy*>(TI->getType())) |
| 126 | return TI; // Accept variable if already of the right type! |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void ListRecTy::print(std::ostream &OS) const { |
| 131 | OS << "list<" << *Ty << ">"; |
| 132 | } |
| 133 | |
| 134 | Init *ListRecTy::convertValue(ListInit *LI) { |
| 135 | std::vector<Init*> Elements; |
| 136 | |
| 137 | // Verify that all of the elements of the list are subclasses of the |
| 138 | // appropriate class! |
| 139 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) |
| 140 | if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty)) |
| 141 | Elements.push_back(CI); |
| 142 | else |
| 143 | return 0; |
| 144 | |
| 145 | return new ListInit(Elements); |
| 146 | } |
| 147 | |
| 148 | Init *ListRecTy::convertValue(TypedInit *TI) { |
| 149 | // Ensure that TI is compatible with our class. |
| 150 | if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType())) |
| 151 | if (LRT->getElementType()->typeIsConvertibleTo(getElementType())) |
| 152 | return TI; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | Init *CodeRecTy::convertValue(TypedInit *TI) { |
| 157 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 158 | return TI; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | Init *DagRecTy::convertValue(TypedInit *TI) { |
| 163 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 164 | return TI; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void RecordRecTy::print(std::ostream &OS) const { |
| 170 | OS << Rec->getName(); |
| 171 | } |
| 172 | |
| 173 | Init *RecordRecTy::convertValue(DefInit *DI) { |
| 174 | // Ensure that DI is a subclass of Rec. |
| 175 | if (!DI->getDef()->isSubClassOf(Rec)) |
| 176 | return 0; |
| 177 | return DI; |
| 178 | } |
| 179 | |
| 180 | Init *RecordRecTy::convertValue(TypedInit *TI) { |
| 181 | // Ensure that TI is compatible with Rec. |
| 182 | if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType())) |
| 183 | if (RRT->getRecord()->isSubClassOf(getRecord()) || |
| 184 | RRT->getRecord() == getRecord()) |
| 185 | return TI; |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const { |
| 190 | return Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | // Initializer implementations |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | |
| 198 | void Init::dump() const { return print(std::cerr); } |
| 199 | |
| 200 | Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
| 201 | BitsInit *BI = new BitsInit(Bits.size()); |
| 202 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 203 | if (Bits[i] >= getNumBits()) { |
| 204 | delete BI; |
| 205 | return 0; |
| 206 | } |
| 207 | BI->setBit(i, getBit(Bits[i])); |
| 208 | } |
| 209 | return BI; |
| 210 | } |
| 211 | |
| 212 | void BitsInit::print(std::ostream &OS) const { |
| 213 | //if (!printInHex(OS)) return; |
| 214 | //if (!printAsVariable(OS)) return; |
| 215 | //if (!printAsUnset(OS)) return; |
| 216 | |
| 217 | OS << "{ "; |
| 218 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
| 219 | if (i) OS << ", "; |
| 220 | if (Init *Bit = getBit(e-i-1)) |
| 221 | Bit->print(OS); |
| 222 | else |
| 223 | OS << "*"; |
| 224 | } |
| 225 | OS << " }"; |
| 226 | } |
| 227 | |
| 228 | bool BitsInit::printInHex(std::ostream &OS) const { |
| 229 | // First, attempt to convert the value into an integer value... |
| 230 | int Result = 0; |
| 231 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) |
| 232 | if (BitInit *Bit = dynamic_cast<BitInit*>(getBit(i))) { |
| 233 | Result |= Bit->getValue() << i; |
| 234 | } else { |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | OS << "0x" << std::hex << Result << std::dec; |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | bool BitsInit::printAsVariable(std::ostream &OS) const { |
| 243 | // Get the variable that we may be set equal to... |
| 244 | assert(getNumBits() != 0); |
| 245 | VarBitInit *FirstBit = dynamic_cast<VarBitInit*>(getBit(0)); |
| 246 | if (FirstBit == 0) return true; |
| 247 | TypedInit *Var = FirstBit->getVariable(); |
| 248 | |
| 249 | // Check to make sure the types are compatible. |
| 250 | BitsRecTy *Ty = dynamic_cast<BitsRecTy*>(FirstBit->getVariable()->getType()); |
| 251 | if (Ty == 0) return true; |
| 252 | if (Ty->getNumBits() != getNumBits()) return true; // Incompatible types! |
| 253 | |
| 254 | // Check to make sure all bits are referring to the right bits in the variable |
| 255 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
| 256 | VarBitInit *Bit = dynamic_cast<VarBitInit*>(getBit(i)); |
| 257 | if (Bit == 0 || Bit->getVariable() != Var || Bit->getBitNum() != i) |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | Var->print(OS); |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | bool BitsInit::printAsUnset(std::ostream &OS) const { |
| 266 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) |
| 267 | if (!dynamic_cast<UnsetInit*>(getBit(i))) |
| 268 | return true; |
| 269 | OS << "?"; |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | // resolveReferences - If there are any field references that refer to fields |
| 274 | // that have been filled in, we can propagate the values now. |
| 275 | // |
| 276 | Init *BitsInit::resolveReferences(Record &R) { |
| 277 | bool Changed = false; |
| 278 | BitsInit *New = new BitsInit(getNumBits()); |
| 279 | |
| 280 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 281 | Init *B; |
| 282 | Init *CurBit = getBit(i); |
| 283 | |
| 284 | do { |
| 285 | B = CurBit; |
| 286 | CurBit = CurBit->resolveReferences(R); |
| 287 | Changed |= B != CurBit; |
| 288 | } while (B != CurBit); |
| 289 | New->setBit(i, CurBit); |
| 290 | } |
| 291 | |
| 292 | if (Changed) |
| 293 | return New; |
| 294 | delete New; |
| 295 | return this; |
| 296 | } |
| 297 | |
| 298 | Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
| 299 | BitsInit *BI = new BitsInit(Bits.size()); |
| 300 | |
| 301 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 302 | if (Bits[i] >= 32) { |
| 303 | delete BI; |
| 304 | return 0; |
| 305 | } |
| 306 | BI->setBit(i, new BitInit(Value & (1 << Bits[i]))); |
| 307 | } |
| 308 | return BI; |
| 309 | } |
| 310 | |
| 311 | void ListInit::print(std::ostream &OS) const { |
| 312 | OS << "["; |
| 313 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
| 314 | if (i) OS << ", "; |
| 315 | OS << *Values[i]; |
| 316 | } |
| 317 | OS << "]"; |
| 318 | } |
| 319 | |
| 320 | Init *VarInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
| 321 | BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType()); |
| 322 | if (T == 0) return 0; // Cannot subscript a non-bits variable... |
| 323 | unsigned NumBits = T->getNumBits(); |
| 324 | |
| 325 | BitsInit *BI = new BitsInit(Bits.size()); |
| 326 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 327 | if (Bits[i] >= NumBits) { |
| 328 | delete BI; |
| 329 | return 0; |
| 330 | } |
| 331 | BI->setBit(i, new VarBitInit(this, Bits[i])); |
| 332 | } |
| 333 | return BI; |
| 334 | } |
| 335 | |
| 336 | Init *VarInit::resolveBitReference(Record &R, unsigned Bit) { |
| 337 | if (R.isTemplateArg(getName())) |
| 338 | return this; |
| 339 | |
| 340 | RecordVal *RV = R.getValue(getName()); |
| 341 | assert(RV && "Reference to a non-existant variable?"); |
| 342 | assert(dynamic_cast<BitsInit*>(RV->getValue())); |
| 343 | BitsInit *BI = (BitsInit*)RV->getValue(); |
| 344 | |
| 345 | assert(Bit < BI->getNumBits() && "Bit reference out of range!"); |
| 346 | Init *B = BI->getBit(Bit); |
| 347 | |
| 348 | if (!dynamic_cast<UnsetInit*>(B)) // If the bit is not set... |
| 349 | return B; // Replace the VarBitInit with it. |
| 350 | return this; |
| 351 | } |
| 352 | |
| 353 | RecTy *VarInit::getFieldType(const std::string &FieldName) const { |
| 354 | if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType())) |
| 355 | if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) |
| 356 | return RV->getType(); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const { |
| 361 | if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType())) |
| 362 | if (const RecordVal *RV = R.getValue(VarName)) |
| 363 | if (Init *I = RV->getValue()->getFieldInit(R, FieldName)) |
| 364 | return I; |
| 365 | else |
| 366 | return 0; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /// resolveReferences - This method is used by classes that refer to other |
| 371 | /// variables which may not be defined at the time they expression is formed. |
| 372 | /// If a value is set for the variable later, this method will be called on |
| 373 | /// users of the value to allow the value to propagate out. |
| 374 | /// |
| 375 | Init *VarInit::resolveReferences(Record &R) { |
| 376 | if (RecordVal *Val = R.getValue(VarName)) |
| 377 | if (!dynamic_cast<UnsetInit*>(Val->getValue())) |
| 378 | return Val->getValue(); |
| 379 | return this; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | Init *VarBitInit::resolveReferences(Record &R) { |
| 384 | Init *I = getVariable()->resolveBitReference(R, getBitNum()); |
| 385 | if (I != getVariable()) |
| 386 | return I; |
| 387 | return this; |
| 388 | } |
| 389 | |
| 390 | RecTy *DefInit::getFieldType(const std::string &FieldName) const { |
| 391 | if (const RecordVal *RV = Def->getValue(FieldName)) |
| 392 | return RV->getType(); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | Init *DefInit::getFieldInit(Record &R, const std::string &FieldName) const { |
| 397 | return Def->getValue(FieldName)->getValue(); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | void DefInit::print(std::ostream &OS) const { |
| 402 | OS << Def->getName(); |
| 403 | } |
| 404 | |
| 405 | Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
| 406 | BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType()); |
| 407 | if (T == 0) return 0; // Cannot subscript a non-bits field... |
| 408 | unsigned NumBits = T->getNumBits(); |
| 409 | |
| 410 | BitsInit *BI = new BitsInit(Bits.size()); |
| 411 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 412 | if (Bits[i] >= NumBits) { |
| 413 | delete BI; |
| 414 | return 0; |
| 415 | } |
| 416 | BI->setBit(i, new VarBitInit(this, Bits[i])); |
| 417 | } |
| 418 | return BI; |
| 419 | } |
| 420 | |
| 421 | Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) { |
| 422 | Init *BitsVal = Rec->getFieldInit(R, FieldName); |
| 423 | if (BitsVal) |
| 424 | if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) { |
| 425 | assert(Bit < BI->getNumBits() && "Bit reference out of range!"); |
| 426 | Init *B = BI->getBit(Bit); |
| 427 | |
| 428 | if (dynamic_cast<BitInit*>(B)) // If the bit is set... |
| 429 | return B; // Replace the VarBitInit with it. |
| 430 | } |
| 431 | return this; |
| 432 | } |
| 433 | |
| 434 | Init *FieldInit::resolveReferences(Record &R) { |
| 435 | Init *BitsVal = Rec->getFieldInit(R, FieldName); |
| 436 | if (BitsVal) { |
| 437 | Init *BVR = BitsVal->resolveReferences(R); |
| 438 | return BVR->isComplete() ? BVR : this; |
| 439 | } |
| 440 | return this; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | void DagInit::print(std::ostream &OS) const { |
| 445 | OS << "(" << NodeTypeDef->getName(); |
| 446 | if (Args.size()) { |
| 447 | OS << " " << *Args[0]; |
| 448 | if (!ArgNames[0].empty()) OS << ":$" << ArgNames[0]; |
| 449 | for (unsigned i = 1, e = Args.size(); i != e; ++i) { |
| 450 | OS << ", " << *Args[i]; |
| 451 | if (!ArgNames[i].empty()) OS << ":$" << ArgNames[i]; |
| 452 | } |
| 453 | } |
| 454 | OS << ")"; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | //===----------------------------------------------------------------------===// |
| 459 | // Other implementations |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
| 462 | RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P) |
| 463 | : Name(N), Ty(T), Prefix(P) { |
| 464 | Value = Ty->convertValue(new UnsetInit()); |
| 465 | assert(Value && "Cannot create unset value for current type!"); |
| 466 | } |
| 467 | |
| 468 | void RecordVal::dump() const { std::cerr << *this; } |
| 469 | |
| 470 | void RecordVal::print(std::ostream &OS, bool PrintSem) const { |
| 471 | if (getPrefix()) OS << "field "; |
| 472 | OS << *getType() << " " << getName(); |
| 473 | if (getValue()) { |
| 474 | OS << " = " << *getValue(); |
| 475 | } |
| 476 | if (PrintSem) OS << ";\n"; |
| 477 | } |
| 478 | |
| 479 | // resolveReferences - If there are any field references that refer to fields |
| 480 | // that have been filled in, we can propagate the values now. |
| 481 | // |
| 482 | void Record::resolveReferences() { |
| 483 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 484 | Values[i].setValue(Values[i].getValue()->resolveReferences(*this)); |
| 485 | } |
| 486 | |
| 487 | void Record::dump() const { std::cerr << *this; } |
| 488 | |
| 489 | std::ostream &operator<<(std::ostream &OS, const Record &R) { |
| 490 | OS << R.getName(); |
| 491 | |
| 492 | const std::vector<std::string> &TArgs = R.getTemplateArgs(); |
| 493 | if (!TArgs.empty()) { |
| 494 | OS << "<"; |
| 495 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 496 | if (i) OS << ", "; |
| 497 | const RecordVal *RV = R.getValue(TArgs[i]); |
| 498 | assert(RV && "Template argument record not found??"); |
| 499 | RV->print(OS, false); |
| 500 | } |
| 501 | OS << ">"; |
| 502 | } |
| 503 | |
| 504 | OS << " {"; |
| 505 | const std::vector<Record*> &SC = R.getSuperClasses(); |
| 506 | if (!SC.empty()) { |
| 507 | OS << "\t//"; |
| 508 | for (unsigned i = 0, e = SC.size(); i != e; ++i) |
| 509 | OS << " " << SC[i]->getName(); |
| 510 | } |
| 511 | OS << "\n"; |
| 512 | |
| 513 | const std::vector<RecordVal> &Vals = R.getValues(); |
| 514 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 515 | if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) |
| 516 | OS << Vals[i]; |
| 517 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 518 | if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) |
| 519 | OS << Vals[i]; |
| 520 | |
| 521 | return OS << "}\n"; |
| 522 | } |
| 523 | |
| 524 | /// getValueInit - Return the initializer for a value with the specified name, |
| 525 | /// or throw an exception if the field does not exist. |
| 526 | /// |
| 527 | Init *Record::getValueInit(const std::string &FieldName) const { |
| 528 | const RecordVal *R = getValue(FieldName); |
| 529 | if (R == 0 || R->getValue() == 0) |
| 530 | throw "Record '" + getName() + "' does not have a field named '" + |
| 531 | FieldName + "!\n"; |
| 532 | return R->getValue(); |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /// getValueAsString - This method looks up the specified field and returns its |
| 537 | /// value as a string, throwing an exception if the field does not exist or if |
| 538 | /// the value is not a string. |
| 539 | /// |
| 540 | std::string Record::getValueAsString(const std::string &FieldName) const { |
| 541 | const RecordVal *R = getValue(FieldName); |
| 542 | if (R == 0 || R->getValue() == 0) |
| 543 | throw "Record '" + getName() + "' does not have a field named '" + |
| 544 | FieldName + "!\n"; |
| 545 | |
| 546 | if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue())) |
| 547 | return SI->getValue(); |
| 548 | throw "Record '" + getName() + "', field '" + FieldName + |
| 549 | "' does not have a string initializer!"; |
| 550 | } |
| 551 | |
| 552 | /// getValueAsBitsInit - This method looks up the specified field and returns |
| 553 | /// its value as a BitsInit, throwing an exception if the field does not exist |
| 554 | /// or if the value is not the right type. |
| 555 | /// |
| 556 | BitsInit *Record::getValueAsBitsInit(const std::string &FieldName) const { |
| 557 | const RecordVal *R = getValue(FieldName); |
| 558 | if (R == 0 || R->getValue() == 0) |
| 559 | throw "Record '" + getName() + "' does not have a field named '" + |
| 560 | FieldName + "!\n"; |
| 561 | |
| 562 | if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue())) |
| 563 | return BI; |
| 564 | throw "Record '" + getName() + "', field '" + FieldName + |
| 565 | "' does not have a BitsInit initializer!"; |
| 566 | } |
| 567 | |
| 568 | /// getValueAsListInit - This method looks up the specified field and returns |
| 569 | /// its value as a ListInit, throwing an exception if the field does not exist |
| 570 | /// or if the value is not the right type. |
| 571 | /// |
| 572 | ListInit *Record::getValueAsListInit(const std::string &FieldName) const { |
| 573 | const RecordVal *R = getValue(FieldName); |
| 574 | if (R == 0 || R->getValue() == 0) |
| 575 | throw "Record '" + getName() + "' does not have a field named '" + |
| 576 | FieldName + "!\n"; |
| 577 | |
| 578 | if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue())) |
| 579 | return LI; |
| 580 | throw "Record '" + getName() + "', field '" + FieldName + |
| 581 | "' does not have a list initializer!"; |
| 582 | } |
| 583 | |
| 584 | /// getValueAsInt - This method looks up the specified field and returns its |
| 585 | /// value as an int, throwing an exception if the field does not exist or if |
| 586 | /// the value is not the right type. |
| 587 | /// |
| 588 | int Record::getValueAsInt(const std::string &FieldName) const { |
| 589 | const RecordVal *R = getValue(FieldName); |
| 590 | if (R == 0 || R->getValue() == 0) |
| 591 | throw "Record '" + getName() + "' does not have a field named '" + |
| 592 | FieldName + "!\n"; |
| 593 | |
| 594 | if (IntInit *II = dynamic_cast<IntInit*>(R->getValue())) |
| 595 | return II->getValue(); |
| 596 | throw "Record '" + getName() + "', field '" + FieldName + |
| 597 | "' does not have a list initializer!"; |
| 598 | } |
| 599 | |
| 600 | /// getValueAsDef - This method looks up the specified field and returns its |
| 601 | /// value as a Record, throwing an exception if the field does not exist or if |
| 602 | /// the value is not the right type. |
| 603 | /// |
| 604 | Record *Record::getValueAsDef(const std::string &FieldName) const { |
| 605 | const RecordVal *R = getValue(FieldName); |
| 606 | if (R == 0 || R->getValue() == 0) |
| 607 | throw "Record '" + getName() + "' does not have a field named '" + |
| 608 | FieldName + "!\n"; |
| 609 | |
| 610 | if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue())) |
| 611 | return DI->getDef(); |
| 612 | throw "Record '" + getName() + "', field '" + FieldName + |
| 613 | "' does not have a list initializer!"; |
| 614 | } |
| 615 | |
| 616 | /// getValueAsBit - This method looks up the specified field and returns its |
| 617 | /// value as a bit, throwing an exception if the field does not exist or if |
| 618 | /// the value is not the right type. |
| 619 | /// |
| 620 | bool Record::getValueAsBit(const std::string &FieldName) const { |
| 621 | const RecordVal *R = getValue(FieldName); |
| 622 | if (R == 0 || R->getValue() == 0) |
| 623 | throw "Record '" + getName() + "' does not have a field named '" + |
| 624 | FieldName + "!\n"; |
| 625 | |
| 626 | if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue())) |
| 627 | return BI->getValue(); |
| 628 | throw "Record '" + getName() + "', field '" + FieldName + |
| 629 | "' does not have a bit initializer!"; |
| 630 | } |
| 631 | |
| 632 | /// getValueAsDag - This method looks up the specified field and returns its |
| 633 | /// value as an Dag, throwing an exception if the field does not exist or if |
| 634 | /// the value is not the right type. |
| 635 | /// |
| 636 | DagInit *Record::getValueAsDag(const std::string &FieldName) const { |
| 637 | const RecordVal *R = getValue(FieldName); |
| 638 | if (R == 0 || R->getValue() == 0) |
| 639 | throw "Record '" + getName() + "' does not have a field named '" + |
| 640 | FieldName + "!\n"; |
| 641 | |
| 642 | if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue())) |
| 643 | return DI; |
| 644 | throw "Record '" + getName() + "', field '" + FieldName + |
| 645 | "' does not have a dag initializer!"; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | void RecordKeeper::dump() const { std::cerr << *this; } |
| 650 | |
| 651 | std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) { |
| 652 | OS << "------------- Classes -----------------\n"; |
| 653 | const std::map<std::string, Record*> &Classes = RK.getClasses(); |
| 654 | for (std::map<std::string, Record*>::const_iterator I = Classes.begin(), |
| 655 | E = Classes.end(); I != E; ++I) |
| 656 | OS << "class " << *I->second; |
| 657 | |
| 658 | OS << "------------- Defs -----------------\n"; |
| 659 | const std::map<std::string, Record*> &Defs = RK.getDefs(); |
| 660 | for (std::map<std::string, Record*>::const_iterator I = Defs.begin(), |
| 661 | E = Defs.end(); I != E; ++I) |
| 662 | OS << "def " << *I->second; |
| 663 | return OS; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /// getAllDerivedDefinitions - This method returns all concrete definitions |
| 668 | /// that derive from the specified class name. If a class with the specified |
| 669 | /// name does not exist, an error is printed and true is returned. |
| 670 | std::vector<Record*> |
| 671 | RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const { |
| 672 | Record *Class = Records.getClass(ClassName); |
| 673 | if (!Class) |
| 674 | throw "ERROR: Couldn't find the '" + ClassName + "' class!\n"; |
| 675 | |
| 676 | std::vector<Record*> Defs; |
| 677 | for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(), |
| 678 | E = getDefs().end(); I != E; ++I) |
| 679 | if (I->second->isSubClassOf(Class)) |
| 680 | Defs.push_back(I->second); |
| 681 | |
| 682 | return Defs; |
| 683 | } |