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