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