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