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