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 | // |
Chris Lattner | 8adcd9f | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 15 | #include "Error.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/DataTypes.h" |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Format.h" |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Duraid Madina | 14492af | 2005-12-26 05:08:55 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 6847866 | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Type implementations |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 26 | void RecTy::dump() const { print(errs()); } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 27 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 28 | Init *BitRecTy::convertValue(BitsInit *BI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 29 | if (BI->getNumBits() != 1) return 0; // Only accept if just one bit! |
| 30 | return BI->getBit(0); |
| 31 | } |
| 32 | |
| 33 | bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const { |
| 34 | return RHS->getNumBits() == 1; |
| 35 | } |
| 36 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 37 | Init *BitRecTy::convertValue(IntInit *II) { |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 38 | int64_t Val = II->getValue(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 39 | 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] | 40 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 41 | return new BitInit(Val != 0); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 44 | Init *BitRecTy::convertValue(TypedInit *VI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 45 | if (dynamic_cast<BitRecTy*>(VI->getType())) |
| 46 | return VI; // Accept variable if it is already of bit type! |
| 47 | return 0; |
| 48 | } |
| 49 | |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 50 | std::string BitsRecTy::getAsString() const { |
| 51 | return "bits<" + utostr(Size) + ">"; |
| 52 | } |
| 53 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 54 | Init *BitsRecTy::convertValue(UnsetInit *UI) { |
| 55 | BitsInit *Ret = new BitsInit(Size); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 56 | |
| 57 | for (unsigned i = 0; i != Size; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 58 | Ret->setBit(i, new UnsetInit()); |
| 59 | return Ret; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 62 | Init *BitsRecTy::convertValue(BitInit *UI) { |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 63 | if (Size != 1) return 0; // Can only convert single bit. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 64 | BitsInit *Ret = new BitsInit(1); |
| 65 | Ret->setBit(0, UI); |
| 66 | return Ret; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 69 | /// canFitInBitfield - Return true if the number of bits is large enough to hold |
| 70 | /// the integer value. |
| 71 | static bool canFitInBitfield(int64_t Value, unsigned NumBits) { |
Nick Lewycky | 79286bf | 2011-06-03 08:25:39 +0000 | [diff] [blame] | 72 | // For example, with NumBits == 4, we permit Values from [-7 .. 15]. |
| 73 | return (NumBits >= sizeof(Value) * 8) || |
| 74 | (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 77 | /// convertValue from Int initializer to bits type: Split the integer up into the |
| 78 | /// appropriate bits. |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 79 | /// |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 80 | Init *BitsRecTy::convertValue(IntInit *II) { |
Misha Brukman | 6752fb5 | 2004-06-21 18:01:47 +0000 | [diff] [blame] | 81 | int64_t Value = II->getValue(); |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 82 | // Make sure this bitfield is large enough to hold the integer value. |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 83 | if (!canFitInBitfield(Value, Size)) |
| 84 | return 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 85 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 86 | BitsInit *Ret = new BitsInit(Size); |
David Greene | af973b4 | 2011-07-11 18:25:51 +0000 | [diff] [blame] | 87 | for (unsigned i = 0; i != Size; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 88 | Ret->setBit(i, new BitInit(Value & (1LL << i))); |
David Greene | af973b4 | 2011-07-11 18:25:51 +0000 | [diff] [blame] | 89 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 90 | return Ret; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 93 | Init *BitsRecTy::convertValue(BitsInit *BI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 94 | // If the number of bits is right, return it. Otherwise we need to expand or |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 95 | // truncate. |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 96 | if (BI->getNumBits() == Size) return BI; |
| 97 | return 0; |
| 98 | } |
| 99 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 100 | Init *BitsRecTy::convertValue(TypedInit *VI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 101 | if (BitsRecTy *BRT = dynamic_cast<BitsRecTy*>(VI->getType())) |
| 102 | if (BRT->Size == Size) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 103 | BitsInit *Ret = new BitsInit(Size); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 104 | for (unsigned i = 0; i != Size; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 105 | Ret->setBit(i, new VarBitInit(VI, i)); |
| 106 | return Ret; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 107 | } |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 108 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 109 | if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 110 | BitsInit *Ret = new BitsInit(1); |
| 111 | Ret->setBit(0, VI); |
| 112 | return Ret; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 113 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 114 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 115 | if (TernOpInit *Tern = dynamic_cast<TernOpInit*>(VI)) { |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 116 | if (Tern->getOpcode() == TernOpInit::IF) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 117 | Init *LHS = Tern->getLHS(); |
| 118 | Init *MHS = Tern->getMHS(); |
| 119 | Init *RHS = Tern->getRHS(); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 120 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 121 | IntInit *MHSi = dynamic_cast<IntInit*>(MHS); |
| 122 | IntInit *RHSi = dynamic_cast<IntInit*>(RHS); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 123 | |
| 124 | if (MHSi && RHSi) { |
| 125 | int64_t MHSVal = MHSi->getValue(); |
| 126 | int64_t RHSVal = RHSi->getValue(); |
| 127 | |
| 128 | if (canFitInBitfield(MHSVal, Size) && canFitInBitfield(RHSVal, Size)) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 129 | BitsInit *Ret = new BitsInit(Size); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 130 | |
| 131 | for (unsigned i = 0; i != Size; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 132 | Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS, |
| 133 | new IntInit((MHSVal & (1LL << i)) ? 1 : 0), |
| 134 | new IntInit((RHSVal & (1LL << i)) ? 1 : 0), |
| 135 | VI->getType())); |
| 136 | |
| 137 | return Ret; |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 138 | } |
| 139 | } else { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 140 | BitsInit *MHSbs = dynamic_cast<BitsInit*>(MHS); |
| 141 | BitsInit *RHSbs = dynamic_cast<BitsInit*>(RHS); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 142 | |
| 143 | if (MHSbs && RHSbs) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 144 | BitsInit *Ret = new BitsInit(Size); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 145 | |
| 146 | for (unsigned i = 0; i != Size; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 147 | Ret->setBit(i, new TernOpInit(TernOpInit::IF, LHS, |
| 148 | MHSbs->getBit(i), |
| 149 | RHSbs->getBit(i), |
| 150 | VI->getType())); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 151 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 152 | return Ret; |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 161 | Init *IntRecTy::convertValue(BitInit *BI) { |
| 162 | return new IntInit(BI->getValue()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 165 | Init *IntRecTy::convertValue(BitsInit *BI) { |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 166 | int64_t Result = 0; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 167 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 168 | if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 169 | Result |= Bit->getValue() << i; |
| 170 | } else { |
| 171 | return 0; |
| 172 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 173 | return new IntInit(Result); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 176 | Init *IntRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 177 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 178 | return TI; // Accept variable if already of the right type! |
| 179 | return 0; |
| 180 | } |
| 181 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 182 | Init *StringRecTy::convertValue(UnOpInit *BO) { |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 183 | if (BO->getOpcode() == UnOpInit::CAST) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 184 | Init *L = BO->getOperand()->convertInitializerTo(this); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 185 | if (L == 0) return 0; |
| 186 | if (L != BO->getOperand()) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 187 | return new UnOpInit(UnOpInit::CAST, L, new StringRecTy); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 188 | return BO; |
| 189 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 190 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 191 | return convertValue((TypedInit*)BO); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 192 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 193 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 194 | Init *StringRecTy::convertValue(BinOpInit *BO) { |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 195 | if (BO->getOpcode() == BinOpInit::STRCONCAT) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 196 | Init *L = BO->getLHS()->convertInitializerTo(this); |
| 197 | Init *R = BO->getRHS()->convertInitializerTo(this); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 198 | if (L == 0 || R == 0) return 0; |
| 199 | if (L != BO->getLHS() || R != BO->getRHS()) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 200 | return new BinOpInit(BinOpInit::STRCONCAT, L, R, new StringRecTy); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 201 | return BO; |
| 202 | } |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 203 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 204 | return convertValue((TypedInit*)BO); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 208 | Init *StringRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 209 | if (dynamic_cast<StringRecTy*>(TI->getType())) |
| 210 | return TI; // Accept variable if already of the right type! |
| 211 | return 0; |
| 212 | } |
| 213 | |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 214 | std::string ListRecTy::getAsString() const { |
| 215 | return "list<" + Ty->getAsString() + ">"; |
| 216 | } |
| 217 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 218 | Init *ListRecTy::convertValue(ListInit *LI) { |
| 219 | std::vector<Init*> Elements; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 220 | |
| 221 | // Verify that all of the elements of the list are subclasses of the |
| 222 | // appropriate class! |
| 223 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 224 | if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty)) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 225 | Elements.push_back(CI); |
| 226 | else |
| 227 | return 0; |
| 228 | |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 229 | ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType()); |
| 230 | if (LType == 0) { |
| 231 | return 0; |
| 232 | } |
| 233 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 234 | return new ListInit(Elements, new ListRecTy(Ty)); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 237 | Init *ListRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 238 | // Ensure that TI is compatible with our class. |
| 239 | if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType())) |
| 240 | if (LRT->getElementType()->typeIsConvertibleTo(getElementType())) |
| 241 | return TI; |
| 242 | return 0; |
| 243 | } |
| 244 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 245 | Init *CodeRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 246 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 247 | return TI; |
| 248 | return 0; |
| 249 | } |
| 250 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 251 | Init *DagRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 252 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 253 | return TI; |
| 254 | return 0; |
| 255 | } |
| 256 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 257 | Init *DagRecTy::convertValue(UnOpInit *BO) { |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 258 | if (BO->getOpcode() == UnOpInit::CAST) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 259 | Init *L = BO->getOperand()->convertInitializerTo(this); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 260 | if (L == 0) return 0; |
| 261 | if (L != BO->getOperand()) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 262 | return new UnOpInit(UnOpInit::CAST, L, new DagRecTy); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 263 | return BO; |
| 264 | } |
| 265 | return 0; |
| 266 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 267 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 268 | Init *DagRecTy::convertValue(BinOpInit *BO) { |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 269 | if (BO->getOpcode() == BinOpInit::CONCAT) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 270 | Init *L = BO->getLHS()->convertInitializerTo(this); |
| 271 | Init *R = BO->getRHS()->convertInitializerTo(this); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 272 | if (L == 0 || R == 0) return 0; |
| 273 | if (L != BO->getLHS() || R != BO->getRHS()) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 274 | return new BinOpInit(BinOpInit::CONCAT, L, R, new DagRecTy); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 275 | return BO; |
| 276 | } |
| 277 | return 0; |
| 278 | } |
| 279 | |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 280 | std::string RecordRecTy::getAsString() const { |
| 281 | return Rec->getName(); |
| 282 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 283 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 284 | Init *RecordRecTy::convertValue(DefInit *DI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 285 | // Ensure that DI is a subclass of Rec. |
| 286 | if (!DI->getDef()->isSubClassOf(Rec)) |
| 287 | return 0; |
| 288 | return DI; |
| 289 | } |
| 290 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 291 | Init *RecordRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 292 | // Ensure that TI is compatible with Rec. |
| 293 | if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType())) |
| 294 | if (RRT->getRecord()->isSubClassOf(getRecord()) || |
| 295 | RRT->getRecord() == getRecord()) |
| 296 | return TI; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const { |
Bruno Cardoso Lopes | deb2002 | 2010-06-17 23:00:16 +0000 | [diff] [blame] | 301 | if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec)) |
| 302 | return true; |
| 303 | |
| 304 | const std::vector<Record*> &SC = Rec->getSuperClasses(); |
| 305 | for (unsigned i = 0, e = SC.size(); i != e; ++i) |
| 306 | if (RHS->getRecord()->isSubClassOf(SC[i])) |
| 307 | return true; |
| 308 | |
| 309 | return false; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 313 | /// resolveTypes - Find a common type that T1 and T2 convert to. |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 314 | /// Return 0 if no such type exists. |
| 315 | /// |
| 316 | RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { |
| 317 | if (!T1->typeIsConvertibleTo(T2)) { |
| 318 | if (!T2->typeIsConvertibleTo(T1)) { |
| 319 | // If one is a Record type, check superclasses |
| 320 | RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1); |
| 321 | if (RecTy1) { |
| 322 | // See if T2 inherits from a type T1 also inherits from |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 323 | const std::vector<Record *> &T1SuperClasses = |
| 324 | RecTy1->getRecord()->getSuperClasses(); |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 325 | for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(), |
| 326 | iend = T1SuperClasses.end(); |
| 327 | i != iend; |
| 328 | ++i) { |
| 329 | RecordRecTy *SuperRecTy1 = new RecordRecTy(*i); |
| 330 | RecTy *NewType1 = resolveTypes(SuperRecTy1, T2); |
| 331 | if (NewType1 != 0) { |
| 332 | if (NewType1 != SuperRecTy1) { |
| 333 | delete SuperRecTy1; |
| 334 | } |
| 335 | return NewType1; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2); |
| 340 | if (RecTy2) { |
| 341 | // See if T1 inherits from a type T2 also inherits from |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 342 | const std::vector<Record *> &T2SuperClasses = |
| 343 | RecTy2->getRecord()->getSuperClasses(); |
| 344 | for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(), |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 345 | iend = T2SuperClasses.end(); |
| 346 | i != iend; |
| 347 | ++i) { |
| 348 | RecordRecTy *SuperRecTy2 = new RecordRecTy(*i); |
| 349 | RecTy *NewType2 = resolveTypes(T1, SuperRecTy2); |
| 350 | if (NewType2 != 0) { |
| 351 | if (NewType2 != SuperRecTy2) { |
| 352 | delete SuperRecTy2; |
| 353 | } |
| 354 | return NewType2; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | return T2; |
| 361 | } |
| 362 | return T1; |
| 363 | } |
| 364 | |
| 365 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | // Initializer implementations |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 370 | void Init::dump() const { return print(errs()); } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 371 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 372 | Init *BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
| 373 | BitsInit *BI = new BitsInit(Bits.size()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 374 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 375 | if (Bits[i] >= getNumBits()) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 376 | delete BI; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 377 | return 0; |
| 378 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 379 | BI->setBit(i, getBit(Bits[i])); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 380 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 381 | return BI; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 384 | std::string BitsInit::getAsString() const { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 385 | std::string Result = "{ "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 386 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 387 | if (i) Result += ", "; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 388 | if (Init *Bit = getBit(e-i-1)) |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 389 | Result += Bit->getAsString(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 390 | else |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 391 | Result += "*"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 393 | return Result + " }"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 396 | // resolveReferences - If there are any field references that refer to fields |
| 397 | // that have been filled in, we can propagate the values now. |
| 398 | // |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 399 | Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 400 | bool Changed = false; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 401 | BitsInit *New = new BitsInit(getNumBits()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 402 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 403 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 404 | Init *B; |
| 405 | Init *CurBit = getBit(i); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 406 | |
| 407 | do { |
| 408 | B = CurBit; |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 409 | CurBit = CurBit->resolveReferences(R, RV); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 410 | Changed |= B != CurBit; |
| 411 | } while (B != CurBit); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 412 | New->setBit(i, CurBit); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | if (Changed) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 416 | return New; |
| 417 | delete New; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 418 | return this; |
| 419 | } |
| 420 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 421 | std::string IntInit::getAsString() const { |
| 422 | return itostr(Value); |
| 423 | } |
| 424 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 425 | Init *IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
| 426 | BitsInit *BI = new BitsInit(Bits.size()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 427 | |
| 428 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 429 | if (Bits[i] >= 64) { |
| 430 | delete BI; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 431 | return 0; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 432 | } |
| 433 | BI->setBit(i, new BitInit(Value & (INT64_C(1) << Bits[i]))); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 434 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 435 | return BI; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 438 | Init *ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) { |
| 439 | std::vector<Init*> Vals; |
Chris Lattner | 8bf9e06 | 2004-07-26 23:21:34 +0000 | [diff] [blame] | 440 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| 441 | if (Elements[i] >= getSize()) |
| 442 | return 0; |
| 443 | Vals.push_back(getElement(Elements[i])); |
| 444 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 445 | return new ListInit(Vals, getType()); |
Chris Lattner | 8bf9e06 | 2004-07-26 23:21:34 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Chris Lattner | cbebe46 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 448 | Record *ListInit::getElementAsRecord(unsigned i) const { |
| 449 | assert(i < Values.size() && "List element index out of range!"); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 450 | DefInit *DI = dynamic_cast<DefInit*>(Values[i]); |
Chris Lattner | cbebe46 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 451 | if (DI == 0) throw "Expected record in list!"; |
| 452 | return DI->getDef(); |
| 453 | } |
| 454 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 455 | Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 456 | std::vector<Init*> Resolved; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 457 | Resolved.reserve(getSize()); |
| 458 | bool Changed = false; |
| 459 | |
| 460 | for (unsigned i = 0, e = getSize(); i != e; ++i) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 461 | Init *E; |
| 462 | Init *CurElt = getElement(i); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 463 | |
| 464 | do { |
| 465 | E = CurElt; |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 466 | CurElt = CurElt->resolveReferences(R, RV); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 467 | Changed |= E != CurElt; |
| 468 | } while (E != CurElt); |
| 469 | Resolved.push_back(E); |
| 470 | } |
| 471 | |
| 472 | if (Changed) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 473 | return new ListInit(Resolved, getType()); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 474 | return this; |
| 475 | } |
| 476 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 477 | Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV, |
| 478 | unsigned Elt) { |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 479 | if (Elt >= getSize()) |
| 480 | return 0; // Out of range reference. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 481 | Init *E = getElement(Elt); |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 482 | // If the element is set to some value, or if we are resolving a reference |
| 483 | // to a specific variable and that variable is explicitly unset, then |
| 484 | // replace the VarListElementInit with it. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 485 | if (IRV || !dynamic_cast<UnsetInit*>(E)) |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 486 | return E; |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 490 | std::string ListInit::getAsString() const { |
| 491 | std::string Result = "["; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 492 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 493 | if (i) Result += ", "; |
| 494 | Result += Values[i]->getAsString(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 496 | return Result + "]"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 499 | Init *OpInit::resolveBitReference(Record &R, const RecordVal *IRV, |
| 500 | unsigned Bit) { |
| 501 | Init *Folded = Fold(&R, 0); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 502 | |
| 503 | if (Folded != this) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 504 | TypedInit *Typed = dynamic_cast<TypedInit *>(Folded); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 505 | if (Typed) { |
| 506 | return Typed->resolveBitReference(R, IRV, Bit); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 507 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 508 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 509 | |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 513 | Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV, |
| 514 | unsigned Elt) { |
| 515 | Init *Folded = Fold(&R, 0); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 516 | |
| 517 | if (Folded != this) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 518 | TypedInit *Typed = dynamic_cast<TypedInit *>(Folded); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 519 | if (Typed) { |
| 520 | return Typed->resolveListElementReference(R, IRV, Elt); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 521 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 522 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 523 | |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 527 | Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) { |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 528 | switch (getOpcode()) { |
| 529 | default: assert(0 && "Unknown unop"); |
| 530 | case CAST: { |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 531 | if (getType()->getAsString() == "string") { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 532 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 533 | if (LHSs) { |
| 534 | return LHSs; |
| 535 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 536 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 537 | DefInit *LHSd = dynamic_cast<DefInit*>(LHS); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 538 | if (LHSd) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 539 | return new StringInit(LHSd->getDef()->getName()); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 540 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 541 | } else { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 542 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 543 | if (LHSs) { |
| 544 | std::string Name = LHSs->getValue(); |
| 545 | |
| 546 | // From TGParser::ParseIDValue |
| 547 | if (CurRec) { |
| 548 | if (const RecordVal *RV = CurRec->getValue(Name)) { |
Chris Lattner | 9402633 | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 549 | if (RV->getType() != getType()) |
| 550 | throw "type mismatch in cast"; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 551 | return new VarInit(Name, RV->getType()); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 552 | } |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 553 | |
| 554 | std::string TemplateArgName = CurRec->getName()+":"+Name; |
| 555 | if (CurRec->isTemplateArg(TemplateArgName)) { |
| 556 | const RecordVal *RV = CurRec->getValue(TemplateArgName); |
| 557 | assert(RV && "Template arg doesn't exist??"); |
| 558 | |
Chris Lattner | 9402633 | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 559 | if (RV->getType() != getType()) |
| 560 | throw "type mismatch in cast"; |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 561 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 562 | return new VarInit(TemplateArgName, RV->getType()); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | if (CurMultiClass) { |
| 567 | std::string MCName = CurMultiClass->Rec.getName()+"::"+Name; |
| 568 | if (CurMultiClass->Rec.isTemplateArg(MCName)) { |
| 569 | const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); |
| 570 | assert(RV && "Template arg doesn't exist??"); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 9402633 | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 572 | if (RV->getType() != getType()) |
| 573 | throw "type mismatch in cast"; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 574 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 575 | return new VarInit(MCName, RV->getType()); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 576 | } |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 577 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 579 | if (Record *D = (CurRec->getRecords()).getDef(Name)) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 580 | return new DefInit(D); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 581 | |
Jim Grosbach | 59ddb73 | 2011-05-06 18:47:45 +0000 | [diff] [blame] | 582 | throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n"); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 583 | } |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 584 | } |
| 585 | break; |
| 586 | } |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 587 | case HEAD: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 588 | ListInit *LHSl = dynamic_cast<ListInit*>(LHS); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 589 | if (LHSl) { |
| 590 | if (LHSl->getSize() == 0) { |
| 591 | assert(0 && "Empty list in car"); |
| 592 | return 0; |
| 593 | } |
| 594 | return LHSl->getElement(0); |
| 595 | } |
| 596 | break; |
| 597 | } |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 598 | case TAIL: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 599 | ListInit *LHSl = dynamic_cast<ListInit*>(LHS); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 600 | if (LHSl) { |
| 601 | if (LHSl->getSize() == 0) { |
| 602 | assert(0 && "Empty list in cdr"); |
| 603 | return 0; |
| 604 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 605 | ListInit *Result = new ListInit(LHSl->begin()+1, LHSl->end(), |
| 606 | LHSl->getType()); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 607 | return Result; |
| 608 | } |
| 609 | break; |
| 610 | } |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 611 | case EMPTY: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 612 | ListInit *LHSl = dynamic_cast<ListInit*>(LHS); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 613 | if (LHSl) { |
| 614 | if (LHSl->getSize() == 0) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 615 | return new IntInit(1); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 616 | } else { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 617 | return new IntInit(0); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 620 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 621 | if (LHSs) { |
| 622 | if (LHSs->getValue().empty()) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 623 | return new IntInit(1); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 624 | } else { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 625 | return new IntInit(0); |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 628 | |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 631 | } |
| 632 | return this; |
| 633 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 634 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 635 | Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 636 | Init *lhs = LHS->resolveReferences(R, RV); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 637 | |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 638 | if (LHS != lhs) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 639 | return (new UnOpInit(getOpcode(), lhs, getType()))->Fold(&R, 0); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 640 | return Fold(&R, 0); |
| 641 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 642 | |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 643 | std::string UnOpInit::getAsString() const { |
| 644 | std::string Result; |
| 645 | switch (Opc) { |
| 646 | case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 647 | case HEAD: Result = "!head"; break; |
| 648 | case TAIL: Result = "!tail"; break; |
| 649 | case EMPTY: Result = "!empty"; break; |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 650 | } |
| 651 | return Result + "(" + LHS->getAsString() + ")"; |
| 652 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 653 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 654 | Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) { |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 655 | switch (getOpcode()) { |
| 656 | default: assert(0 && "Unknown binop"); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 657 | case CONCAT: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 658 | DagInit *LHSs = dynamic_cast<DagInit*>(LHS); |
| 659 | DagInit *RHSs = dynamic_cast<DagInit*>(RHS); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 660 | if (LHSs && RHSs) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 661 | DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator()); |
| 662 | DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator()); |
Chris Lattner | 81fd1f2 | 2010-03-18 21:07:51 +0000 | [diff] [blame] | 663 | if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef()) |
| 664 | throw "Concated Dag operators do not match!"; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 665 | std::vector<Init*> Args; |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 666 | std::vector<std::string> ArgNames; |
| 667 | for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) { |
| 668 | Args.push_back(LHSs->getArg(i)); |
| 669 | ArgNames.push_back(LHSs->getArgName(i)); |
| 670 | } |
| 671 | for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) { |
| 672 | Args.push_back(RHSs->getArg(i)); |
| 673 | ArgNames.push_back(RHSs->getArgName(i)); |
| 674 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 675 | return new DagInit(LHSs->getOperator(), "", Args, ArgNames); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 676 | } |
| 677 | break; |
| 678 | } |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 679 | case STRCONCAT: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 680 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
| 681 | StringInit *RHSs = dynamic_cast<StringInit*>(RHS); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 682 | if (LHSs && RHSs) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 683 | return new StringInit(LHSs->getValue() + RHSs->getValue()); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 684 | break; |
| 685 | } |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 686 | case EQ: { |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 687 | // try to fold eq comparison for 'bit' and 'int', otherwise fallback |
| 688 | // to string objects. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 689 | IntInit* L = |
| 690 | dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy())); |
| 691 | IntInit* R = |
| 692 | dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy())); |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 693 | |
| 694 | if (L && R) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 695 | return new IntInit(L->getValue() == R->getValue()); |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 696 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 697 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
| 698 | StringInit *RHSs = dynamic_cast<StringInit*>(RHS); |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 699 | |
| 700 | // Make sure we've resolved |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 701 | if (LHSs && RHSs) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 702 | return new IntInit(LHSs->getValue() == RHSs->getValue()); |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 703 | |
| 704 | break; |
| 705 | } |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 706 | case SHL: |
| 707 | case SRA: |
| 708 | case SRL: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 709 | IntInit *LHSi = dynamic_cast<IntInit*>(LHS); |
| 710 | IntInit *RHSi = dynamic_cast<IntInit*>(RHS); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 711 | if (LHSi && RHSi) { |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 712 | int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); |
| 713 | int64_t Result; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 714 | switch (getOpcode()) { |
| 715 | default: assert(0 && "Bad opcode!"); |
| 716 | case SHL: Result = LHSv << RHSv; break; |
| 717 | case SRA: Result = LHSv >> RHSv; break; |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 718 | case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 719 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 720 | return new IntInit(Result); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 721 | } |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | return this; |
| 726 | } |
| 727 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 728 | Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 729 | Init *lhs = LHS->resolveReferences(R, RV); |
| 730 | Init *rhs = RHS->resolveReferences(R, RV); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 732 | if (LHS != lhs || RHS != rhs) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 733 | return (new BinOpInit(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0); |
David Greene | a9c6c5d | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 734 | return Fold(&R, 0); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 737 | std::string BinOpInit::getAsString() const { |
| 738 | std::string Result; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 739 | switch (Opc) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 740 | case CONCAT: Result = "!con"; break; |
| 741 | case SHL: Result = "!shl"; break; |
| 742 | case SRA: Result = "!sra"; break; |
| 743 | case SRL: Result = "!srl"; break; |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 744 | case EQ: Result = "!eq"; break; |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 745 | case STRCONCAT: Result = "!strconcat"; break; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 746 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 747 | return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 750 | static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, |
| 751 | Record *CurRec, MultiClass *CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 752 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 753 | static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, |
| 754 | RecTy *Type, Record *CurRec, |
| 755 | MultiClass *CurMultiClass) { |
| 756 | std::vector<Init *> NewOperands; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 757 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 758 | TypedInit *TArg = dynamic_cast<TypedInit*>(Arg); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 759 | |
| 760 | // If this is a dag, recurse |
| 761 | if (TArg && TArg->getType()->getAsString() == "dag") { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 762 | Init *Result = ForeachHelper(LHS, Arg, RHSo, Type, |
| 763 | CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 764 | if (Result != 0) { |
| 765 | return Result; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 766 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 767 | return 0; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | for (int i = 0; i < RHSo->getNumOperands(); ++i) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 772 | OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i)); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 773 | |
| 774 | if (RHSoo) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 775 | Init *Result = EvaluateOperation(RHSoo, LHS, Arg, |
| 776 | Type, CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 777 | if (Result != 0) { |
| 778 | NewOperands.push_back(Result); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 779 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 780 | NewOperands.push_back(Arg); |
| 781 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 782 | } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 783 | NewOperands.push_back(Arg); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 784 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 785 | NewOperands.push_back(RHSo->getOperand(i)); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | // Now run the operator and use its result as the new leaf |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 790 | OpInit *NewOp = RHSo->clone(NewOperands); |
| 791 | Init *NewVal = NewOp->Fold(CurRec, CurMultiClass); |
| 792 | if (NewVal != NewOp) { |
| 793 | delete NewOp; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 794 | return NewVal; |
| 795 | } |
| 796 | return 0; |
| 797 | } |
| 798 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 799 | static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, |
| 800 | Record *CurRec, MultiClass *CurMultiClass) { |
| 801 | DagInit *MHSd = dynamic_cast<DagInit*>(MHS); |
| 802 | ListInit *MHSl = dynamic_cast<ListInit*>(MHS); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 803 | |
| 804 | DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type); |
| 805 | ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type); |
| 806 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 807 | OpInit *RHSo = dynamic_cast<OpInit*>(RHS); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 808 | |
| 809 | if (!RHSo) { |
Jim Grosbach | 59ddb73 | 2011-05-06 18:47:45 +0000 | [diff] [blame] | 810 | throw TGError(CurRec->getLoc(), "!foreach requires an operator\n"); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 813 | TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 814 | |
| 815 | if (!LHSt) { |
Jim Grosbach | 59ddb73 | 2011-05-06 18:47:45 +0000 | [diff] [blame] | 816 | throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n"); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Nick Lewycky | 9429822 | 2009-05-15 03:07:14 +0000 | [diff] [blame] | 819 | if ((MHSd && DagType) || (MHSl && ListType)) { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 820 | if (MHSd) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 821 | Init *Val = MHSd->getOperator(); |
| 822 | Init *Result = EvaluateOperation(RHSo, LHS, Val, |
| 823 | Type, CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 824 | if (Result != 0) { |
| 825 | Val = Result; |
| 826 | } |
| 827 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 828 | std::vector<std::pair<Init *, std::string> > args; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 829 | for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 830 | Init *Arg; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 831 | std::string ArgName; |
| 832 | Arg = MHSd->getArg(i); |
| 833 | ArgName = MHSd->getArgName(i); |
| 834 | |
| 835 | // Process args |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 836 | Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type, |
| 837 | CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 838 | if (Result != 0) { |
| 839 | Arg = Result; |
| 840 | } |
| 841 | |
| 842 | // TODO: Process arg names |
| 843 | args.push_back(std::make_pair(Arg, ArgName)); |
| 844 | } |
| 845 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 846 | return new DagInit(Val, "", args); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 847 | } |
| 848 | if (MHSl) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 849 | std::vector<Init *> NewOperands; |
| 850 | std::vector<Init *> NewList(MHSl->begin(), MHSl->end()); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 851 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 852 | for (ListInit::iterator li = NewList.begin(), |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 853 | liend = NewList.end(); |
| 854 | li != liend; |
| 855 | ++li) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 856 | Init *Item = *li; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 857 | NewOperands.clear(); |
| 858 | for(int i = 0; i < RHSo->getNumOperands(); ++i) { |
| 859 | // First, replace the foreach variable with the list item |
| 860 | if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { |
| 861 | NewOperands.push_back(Item); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 862 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 863 | NewOperands.push_back(RHSo->getOperand(i)); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // Now run the operator and use its result as the new list item |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 868 | OpInit *NewOp = RHSo->clone(NewOperands); |
| 869 | Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 870 | if (NewItem != NewOp) { |
| 871 | *li = NewItem; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 872 | delete NewOp; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 875 | return new ListInit(NewList, MHSl->getType()); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | return 0; |
| 879 | } |
| 880 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 881 | Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) { |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 882 | switch (getOpcode()) { |
| 883 | default: assert(0 && "Unknown binop"); |
| 884 | case SUBST: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 885 | DefInit *LHSd = dynamic_cast<DefInit*>(LHS); |
| 886 | VarInit *LHSv = dynamic_cast<VarInit*>(LHS); |
| 887 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 888 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 889 | DefInit *MHSd = dynamic_cast<DefInit*>(MHS); |
| 890 | VarInit *MHSv = dynamic_cast<VarInit*>(MHS); |
| 891 | StringInit *MHSs = dynamic_cast<StringInit*>(MHS); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 892 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 893 | DefInit *RHSd = dynamic_cast<DefInit*>(RHS); |
| 894 | VarInit *RHSv = dynamic_cast<VarInit*>(RHS); |
| 895 | StringInit *RHSs = dynamic_cast<StringInit*>(RHS); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 896 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 897 | if ((LHSd && MHSd && RHSd) |
| 898 | || (LHSv && MHSv && RHSv) |
| 899 | || (LHSs && MHSs && RHSs)) { |
| 900 | if (RHSd) { |
| 901 | Record *Val = RHSd->getDef(); |
| 902 | if (LHSd->getAsString() == RHSd->getAsString()) { |
| 903 | Val = MHSd->getDef(); |
| 904 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 905 | return new DefInit(Val); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 906 | } |
| 907 | if (RHSv) { |
| 908 | std::string Val = RHSv->getName(); |
| 909 | if (LHSv->getAsString() == RHSv->getAsString()) { |
| 910 | Val = MHSv->getName(); |
| 911 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 912 | return new VarInit(Val, getType()); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 913 | } |
| 914 | if (RHSs) { |
| 915 | std::string Val = RHSs->getValue(); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 916 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 917 | std::string::size_type found; |
David Greene | dbf7074 | 2009-12-21 21:21:34 +0000 | [diff] [blame] | 918 | std::string::size_type idx = 0; |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 919 | do { |
David Greene | dbf7074 | 2009-12-21 21:21:34 +0000 | [diff] [blame] | 920 | found = Val.find(LHSs->getValue(), idx); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 921 | if (found != std::string::npos) { |
| 922 | Val.replace(found, LHSs->getValue().size(), MHSs->getValue()); |
| 923 | } |
David Greene | dbf7074 | 2009-12-21 21:21:34 +0000 | [diff] [blame] | 924 | idx = found + MHSs->getValue().size(); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 925 | } while (found != std::string::npos); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 926 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 927 | return new StringInit(Val); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | break; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 931 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 932 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 933 | case FOREACH: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 934 | Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 935 | CurRec, CurMultiClass); |
| 936 | if (Result != 0) { |
| 937 | return Result; |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 938 | } |
| 939 | break; |
| 940 | } |
David Greene | 3587eed | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 941 | |
| 942 | case IF: { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 943 | IntInit *LHSi = dynamic_cast<IntInit*>(LHS); |
| 944 | if (Init *I = LHS->convertInitializerTo(new IntRecTy())) |
| 945 | LHSi = dynamic_cast<IntInit*>(I); |
David Greene | 3587eed | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 946 | if (LHSi) { |
| 947 | if (LHSi->getValue()) { |
| 948 | return MHS; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 949 | } else { |
David Greene | 3587eed | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 950 | return RHS; |
| 951 | } |
| 952 | } |
| 953 | break; |
| 954 | } |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 955 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 956 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 957 | return this; |
| 958 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 959 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 960 | Init *TernOpInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 961 | Init *lhs = LHS->resolveReferences(R, RV); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 962 | |
| 963 | if (Opc == IF && lhs != LHS) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 964 | IntInit *Value = dynamic_cast<IntInit*>(lhs); |
| 965 | if (Init *I = lhs->convertInitializerTo(new IntRecTy())) |
| 966 | Value = dynamic_cast<IntInit*>(I); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 967 | if (Value != 0) { |
| 968 | // Short-circuit |
| 969 | if (Value->getValue()) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 970 | Init *mhs = MHS->resolveReferences(R, RV); |
| 971 | return (new TernOpInit(getOpcode(), lhs, mhs, |
| 972 | RHS, getType()))->Fold(&R, 0); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 973 | } else { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 974 | Init *rhs = RHS->resolveReferences(R, RV); |
| 975 | return (new TernOpInit(getOpcode(), lhs, MHS, |
| 976 | rhs, getType()))->Fold(&R, 0); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 980 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 981 | Init *mhs = MHS->resolveReferences(R, RV); |
| 982 | Init *rhs = RHS->resolveReferences(R, RV); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 983 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 984 | if (LHS != lhs || MHS != mhs || RHS != rhs) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 985 | return (new TernOpInit(getOpcode(), lhs, mhs, rhs, getType()))->Fold(&R, 0); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 986 | return Fold(&R, 0); |
| 987 | } |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 988 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 989 | std::string TernOpInit::getAsString() const { |
| 990 | std::string Result; |
| 991 | switch (Opc) { |
| 992 | case SUBST: Result = "!subst"; break; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 993 | case FOREACH: Result = "!foreach"; break; |
| 994 | case IF: Result = "!if"; break; |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 995 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 996 | return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 997 | + RHS->getAsString() + ")"; |
| 998 | } |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 999 | |
David Greene | 2a9de4d | 2010-09-03 21:00:49 +0000 | [diff] [blame] | 1000 | RecTy *TypedInit::getFieldType(const std::string &FieldName) const { |
| 1001 | RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType()); |
| 1002 | if (RecordType) { |
| 1003 | RecordVal *Field = RecordType->getRecord()->getValue(FieldName); |
| 1004 | if (Field) { |
| 1005 | return Field->getType(); |
| 1006 | } |
| 1007 | } |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1011 | Init *TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1012 | BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType()); |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 1013 | if (T == 0) return 0; // Cannot subscript a non-bits variable. |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1014 | unsigned NumBits = T->getNumBits(); |
| 1015 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1016 | BitsInit *BI = new BitsInit(Bits.size()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1017 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
| 1018 | if (Bits[i] >= NumBits) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1019 | delete BI; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1020 | return 0; |
| 1021 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1022 | BI->setBit(i, new VarBitInit(this, Bits[i])); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1023 | } |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1024 | return BI; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1027 | Init *TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) { |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1028 | ListRecTy *T = dynamic_cast<ListRecTy*>(getType()); |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 1029 | if (T == 0) return 0; // Cannot subscript a non-list variable. |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (Elements.size() == 1) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1032 | return new VarListElementInit(this, Elements[0]); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1033 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1034 | std::vector<Init*> ListInits; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1035 | ListInits.reserve(Elements.size()); |
| 1036 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1037 | ListInits.push_back(new VarListElementInit(this, Elements[i])); |
| 1038 | return new ListInit(ListInits, T); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1042 | Init *VarInit::resolveBitReference(Record &R, const RecordVal *IRV, |
| 1043 | unsigned Bit) { |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1044 | if (R.isTemplateArg(getName())) return 0; |
| 1045 | if (IRV && IRV->getName() != getName()) return 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1046 | |
| 1047 | RecordVal *RV = R.getValue(getName()); |
Bob Wilson | 159905a | 2009-11-21 22:44:20 +0000 | [diff] [blame] | 1048 | assert(RV && "Reference to a non-existent variable?"); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1049 | assert(dynamic_cast<BitsInit*>(RV->getValue())); |
| 1050 | BitsInit *BI = (BitsInit*)RV->getValue(); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1052 | assert(Bit < BI->getNumBits() && "Bit reference out of range!"); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1053 | Init *B = BI->getBit(Bit); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1054 | |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1055 | // If the bit is set to some value, or if we are resolving a reference to a |
| 1056 | // specific variable and that variable is explicitly unset, then replace the |
| 1057 | // VarBitInit with it. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1058 | if (IRV || !dynamic_cast<UnsetInit*>(B)) |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1059 | return B; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1060 | return 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1063 | Init *VarInit::resolveListElementReference(Record &R, const RecordVal *IRV, |
| 1064 | unsigned Elt) { |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1065 | if (R.isTemplateArg(getName())) return 0; |
| 1066 | if (IRV && IRV->getName() != getName()) return 0; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1067 | |
| 1068 | RecordVal *RV = R.getValue(getName()); |
Bob Wilson | 159905a | 2009-11-21 22:44:20 +0000 | [diff] [blame] | 1069 | assert(RV && "Reference to a non-existent variable?"); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1070 | ListInit *LI = dynamic_cast<ListInit*>(RV->getValue()); |
David Greene | 9d3febe | 2009-05-14 20:38:52 +0000 | [diff] [blame] | 1071 | if (!LI) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1072 | VarInit *VI = dynamic_cast<VarInit*>(RV->getValue()); |
David Greene | 9d3febe | 2009-05-14 20:38:52 +0000 | [diff] [blame] | 1073 | assert(VI && "Invalid list element!"); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1074 | return new VarListElementInit(VI, Elt); |
David Greene | 9d3febe | 2009-05-14 20:38:52 +0000 | [diff] [blame] | 1075 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1076 | |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1077 | if (Elt >= LI->getSize()) |
| 1078 | return 0; // Out of range reference. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1079 | Init *E = LI->getElement(Elt); |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1080 | // If the element is set to some value, or if we are resolving a reference |
| 1081 | // to a specific variable and that variable is explicitly unset, then |
| 1082 | // replace the VarListElementInit with it. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1083 | if (IRV || !dynamic_cast<UnsetInit*>(E)) |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1084 | return E; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1089 | RecTy *VarInit::getFieldType(const std::string &FieldName) const { |
| 1090 | if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType())) |
| 1091 | if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) |
| 1092 | return RV->getType(); |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1096 | Init *VarInit::getFieldInit(Record &R, const RecordVal *RV, |
| 1097 | const std::string &FieldName) const { |
Reid Spencer | 2a82686 | 2006-11-02 20:46:16 +0000 | [diff] [blame] | 1098 | if (dynamic_cast<RecordRecTy*>(getType())) |
Jakob Stoklund Olesen | 0e45762 | 2010-03-25 06:23:34 +0000 | [diff] [blame] | 1099 | if (const RecordVal *Val = R.getValue(VarName)) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1100 | if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue()))) |
Jakob Stoklund Olesen | 0e45762 | 2010-03-25 06:23:34 +0000 | [diff] [blame] | 1101 | return 0; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1102 | Init *TheInit = Val->getValue(); |
Chris Lattner | d959ab9 | 2004-02-28 16:31:53 +0000 | [diff] [blame] | 1103 | assert(TheInit != this && "Infinite loop detected!"); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1104 | if (Init *I = TheInit->getFieldInit(R, RV, FieldName)) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1105 | return I; |
| 1106 | else |
| 1107 | return 0; |
Chris Lattner | d959ab9 | 2004-02-28 16:31:53 +0000 | [diff] [blame] | 1108 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | /// resolveReferences - This method is used by classes that refer to other |
Bob Wilson | 159905a | 2009-11-21 22:44:20 +0000 | [diff] [blame] | 1113 | /// variables which may not be defined at the time the expression is formed. |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1114 | /// If a value is set for the variable later, this method will be called on |
| 1115 | /// users of the value to allow the value to propagate out. |
| 1116 | /// |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1117 | Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1118 | if (RecordVal *Val = R.getValue(VarName)) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1119 | if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue()))) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1120 | return Val->getValue(); |
| 1121 | return this; |
| 1122 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1124 | std::string VarBitInit::getAsString() const { |
| 1125 | return TI->getAsString() + "{" + utostr(Bit) + "}"; |
| 1126 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1127 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1128 | Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 1129 | if (Init *I = getVariable()->resolveBitReference(R, RV, getBitNum())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1130 | return I; |
| 1131 | return this; |
| 1132 | } |
| 1133 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1134 | std::string VarListElementInit::getAsString() const { |
| 1135 | return TI->getAsString() + "[" + utostr(Element) + "]"; |
| 1136 | } |
| 1137 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1138 | Init *VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 1139 | if (Init *I = getVariable()->resolveListElementReference(R, RV, |
| 1140 | getElementNum())) |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1141 | return I; |
| 1142 | return this; |
| 1143 | } |
| 1144 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1145 | Init *VarListElementInit::resolveBitReference(Record &R, const RecordVal *RV, |
| 1146 | unsigned Bit) { |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1147 | // FIXME: This should be implemented, to support references like: |
| 1148 | // bit B = AA[0]{1}; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1152 | Init *VarListElementInit:: |
| 1153 | resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) { |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1154 | // FIXME: This should be implemented, to support references like: |
| 1155 | // int B = AA[0][1]; |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1159 | RecTy *DefInit::getFieldType(const std::string &FieldName) const { |
| 1160 | if (const RecordVal *RV = Def->getValue(FieldName)) |
| 1161 | return RV->getType(); |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1165 | Init *DefInit::getFieldInit(Record &R, const RecordVal *RV, |
| 1166 | const std::string &FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1167 | return Def->getValue(FieldName)->getValue(); |
| 1168 | } |
| 1169 | |
| 1170 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1171 | std::string DefInit::getAsString() const { |
| 1172 | return Def->getName(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1175 | Init *FieldInit::resolveBitReference(Record &R, const RecordVal *RV, |
| 1176 | unsigned Bit) { |
| 1177 | if (Init *BitsVal = Rec->getFieldInit(R, RV, FieldName)) |
| 1178 | if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) { |
David Greene | af973b4 | 2011-07-11 18:25:51 +0000 | [diff] [blame] | 1179 | assert(Bit < BI->getNumBits() && "Bit reference out of range!"); |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1180 | Init *B = BI->getBit(Bit); |
David Greene | af973b4 | 2011-07-11 18:25:51 +0000 | [diff] [blame] | 1181 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1182 | if (dynamic_cast<BitInit*>(B)) // If the bit is set. |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1183 | return B; // Replace the VarBitInit with it. |
| 1184 | } |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1185 | return 0; |
| 1186 | } |
| 1187 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1188 | Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV, |
| 1189 | unsigned Elt) { |
| 1190 | if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName)) |
| 1191 | if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) { |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1192 | if (Elt >= LI->getSize()) return 0; |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1193 | Init *E = LI->getElement(Elt); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1194 | |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1195 | // If the element is set to some value, or if we are resolving a |
| 1196 | // reference to a specific variable and that variable is explicitly |
| 1197 | // unset, then replace the VarListElementInit with it. |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1198 | if (RV || !dynamic_cast<UnsetInit*>(E)) |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1199 | return E; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1200 | } |
| 1201 | return 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1204 | Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 1205 | Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec; |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1206 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1207 | Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1208 | if (BitsVal) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1209 | Init *BVR = BitsVal->resolveReferences(R, RV); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1210 | return BVR->isComplete() ? BVR : this; |
| 1211 | } |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1212 | |
| 1213 | if (NewRec != Rec) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1214 | return new FieldInit(NewRec, FieldName); |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1215 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1216 | return this; |
| 1217 | } |
| 1218 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1219 | Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) { |
| 1220 | std::vector<Init*> NewArgs; |
Chris Lattner | 0d3ef40 | 2006-01-31 06:02:35 +0000 | [diff] [blame] | 1221 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 1222 | NewArgs.push_back(Args[i]->resolveReferences(R, RV)); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1223 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1224 | Init *Op = Val->resolveReferences(R, RV); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | b59cf3c | 2006-03-30 22:50:40 +0000 | [diff] [blame] | 1226 | if (Args != NewArgs || Op != Val) |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1227 | return new DagInit(Op, ValName, NewArgs, ArgNames); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 0d3ef40 | 2006-01-31 06:02:35 +0000 | [diff] [blame] | 1229 | return this; |
| 1230 | } |
| 1231 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1233 | std::string DagInit::getAsString() const { |
| 1234 | std::string Result = "(" + Val->getAsString(); |
Nate Begeman | dbe3f77 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1235 | if (!ValName.empty()) |
| 1236 | Result += ":" + ValName; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1237 | if (Args.size()) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1238 | Result += " " + Args[0]->getAsString(); |
| 1239 | if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0]; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1240 | for (unsigned i = 1, e = Args.size(); i != e; ++i) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1241 | Result += ", " + Args[i]->getAsString(); |
| 1242 | if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i]; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1245 | return Result + ")"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | //===----------------------------------------------------------------------===// |
| 1250 | // Other implementations |
| 1251 | //===----------------------------------------------------------------------===// |
| 1252 | |
| 1253 | RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P) |
| 1254 | : Name(N), Ty(T), Prefix(P) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1255 | Value = Ty->convertValue(new UnsetInit()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1256 | assert(Value && "Cannot create unset value for current type!"); |
| 1257 | } |
| 1258 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1259 | void RecordVal::dump() const { errs() << *this; } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1260 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1261 | void RecordVal::print(raw_ostream &OS, bool PrintSem) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1262 | if (getPrefix()) OS << "field "; |
| 1263 | OS << *getType() << " " << getName(); |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1264 | |
| 1265 | if (getValue()) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1266 | OS << " = " << *getValue(); |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1268 | if (PrintSem) OS << ";\n"; |
| 1269 | } |
| 1270 | |
Daniel Dunbar | ced0081 | 2009-08-23 09:47:37 +0000 | [diff] [blame] | 1271 | unsigned Record::LastID = 0; |
| 1272 | |
Chris Lattner | ac28425 | 2005-08-19 17:58:11 +0000 | [diff] [blame] | 1273 | void Record::setName(const std::string &Name) { |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1274 | if (TrackedRecords.getDef(getName()) == this) { |
| 1275 | TrackedRecords.removeDef(getName()); |
Chris Lattner | ac28425 | 2005-08-19 17:58:11 +0000 | [diff] [blame] | 1276 | this->Name = Name; |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1277 | TrackedRecords.addDef(this); |
Chris Lattner | ac28425 | 2005-08-19 17:58:11 +0000 | [diff] [blame] | 1278 | } else { |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1279 | TrackedRecords.removeClass(getName()); |
Chris Lattner | ac28425 | 2005-08-19 17:58:11 +0000 | [diff] [blame] | 1280 | this->Name = Name; |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1281 | TrackedRecords.addClass(this); |
Chris Lattner | ac28425 | 2005-08-19 17:58:11 +0000 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1285 | /// resolveReferencesTo - If anything in this record refers to RV, replace the |
| 1286 | /// reference to RV with the RHS of RV. If RV is null, we resolve all possible |
| 1287 | /// references. |
| 1288 | void Record::resolveReferencesTo(const RecordVal *RV) { |
| 1289 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1290 | if (Init *V = Values[i].getValue()) |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1291 | Values[i].setValue(V->resolveReferences(*this, RV)); |
| 1292 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1295 | void Record::dump() const { errs() << *this; } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1296 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1297 | raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1298 | OS << R.getName(); |
| 1299 | |
| 1300 | const std::vector<std::string> &TArgs = R.getTemplateArgs(); |
| 1301 | if (!TArgs.empty()) { |
| 1302 | OS << "<"; |
| 1303 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 1304 | if (i) OS << ", "; |
| 1305 | const RecordVal *RV = R.getValue(TArgs[i]); |
| 1306 | assert(RV && "Template argument record not found??"); |
| 1307 | RV->print(OS, false); |
| 1308 | } |
| 1309 | OS << ">"; |
| 1310 | } |
| 1311 | |
| 1312 | OS << " {"; |
| 1313 | const std::vector<Record*> &SC = R.getSuperClasses(); |
| 1314 | if (!SC.empty()) { |
| 1315 | OS << "\t//"; |
| 1316 | for (unsigned i = 0, e = SC.size(); i != e; ++i) |
| 1317 | OS << " " << SC[i]->getName(); |
| 1318 | } |
| 1319 | OS << "\n"; |
| 1320 | |
| 1321 | const std::vector<RecordVal> &Vals = R.getValues(); |
| 1322 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 1323 | if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) |
| 1324 | OS << Vals[i]; |
| 1325 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 1326 | if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) |
| 1327 | OS << Vals[i]; |
| 1328 | |
| 1329 | return OS << "}\n"; |
| 1330 | } |
| 1331 | |
| 1332 | /// getValueInit - Return the initializer for a value with the specified name, |
| 1333 | /// or throw an exception if the field does not exist. |
| 1334 | /// |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1335 | Init *Record::getValueInit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1336 | const RecordVal *R = getValue(FieldName); |
| 1337 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1338 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1339 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1340 | return R->getValue(); |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | /// getValueAsString - This method looks up the specified field and returns its |
| 1345 | /// value as a string, throwing an exception if the field does not exist or if |
| 1346 | /// the value is not a string. |
| 1347 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1348 | std::string Record::getValueAsString(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1349 | const RecordVal *R = getValue(FieldName); |
| 1350 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1351 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1352 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue())) |
| 1355 | return SI->getValue(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1356 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1357 | "' does not have a string initializer!"; |
| 1358 | } |
| 1359 | |
| 1360 | /// getValueAsBitsInit - This method looks up the specified field and returns |
| 1361 | /// its value as a BitsInit, throwing an exception if the field does not exist |
| 1362 | /// or if the value is not the right type. |
| 1363 | /// |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1364 | BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1365 | const RecordVal *R = getValue(FieldName); |
| 1366 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1367 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1368 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1369 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1370 | if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1371 | return BI; |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1372 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1373 | "' does not have a BitsInit initializer!"; |
| 1374 | } |
| 1375 | |
| 1376 | /// getValueAsListInit - This method looks up the specified field and returns |
| 1377 | /// its value as a ListInit, throwing an exception if the field does not exist |
| 1378 | /// or if the value is not the right type. |
| 1379 | /// |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1380 | ListInit *Record::getValueAsListInit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1381 | const RecordVal *R = getValue(FieldName); |
| 1382 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1383 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1384 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1385 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1386 | if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1387 | return LI; |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1388 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1389 | "' does not have a list initializer!"; |
| 1390 | } |
| 1391 | |
Chris Lattner | 7ad0bed | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 1392 | /// getValueAsListOfDefs - This method looks up the specified field and returns |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1393 | /// its value as a vector of records, throwing an exception if the field does |
| 1394 | /// not exist or if the value is not the right type. |
| 1395 | /// |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1396 | std::vector<Record*> |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1397 | Record::getValueAsListOfDefs(StringRef FieldName) const { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1398 | ListInit *List = getValueAsListInit(FieldName); |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1399 | std::vector<Record*> Defs; |
| 1400 | for (unsigned i = 0; i < List->getSize(); i++) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1401 | if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) { |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1402 | Defs.push_back(DI->getDef()); |
| 1403 | } else { |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1404 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1405 | "' list is not entirely DefInit!"; |
| 1406 | } |
| 1407 | } |
| 1408 | return Defs; |
| 1409 | } |
| 1410 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1411 | /// getValueAsInt - This method looks up the specified field and returns its |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 1412 | /// value as an int64_t, throwing an exception if the field does not exist or if |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1413 | /// the value is not the right type. |
| 1414 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1415 | int64_t Record::getValueAsInt(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1416 | const RecordVal *R = getValue(FieldName); |
| 1417 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1418 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1419 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1420 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1421 | if (IntInit *II = dynamic_cast<IntInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1422 | return II->getValue(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1423 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Nate Begeman | f621b33 | 2005-11-30 18:37:14 +0000 | [diff] [blame] | 1424 | "' does not have an int initializer!"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1427 | /// getValueAsListOfInts - This method looks up the specified field and returns |
| 1428 | /// its value as a vector of integers, throwing an exception if the field does |
| 1429 | /// not exist or if the value is not the right type. |
| 1430 | /// |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1431 | std::vector<int64_t> |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1432 | Record::getValueAsListOfInts(StringRef FieldName) const { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1433 | ListInit *List = getValueAsListInit(FieldName); |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 1434 | std::vector<int64_t> Ints; |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1435 | for (unsigned i = 0; i < List->getSize(); i++) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1436 | if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) { |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1437 | Ints.push_back(II->getValue()); |
| 1438 | } else { |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1439 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1440 | "' does not have a list of ints initializer!"; |
| 1441 | } |
| 1442 | } |
| 1443 | return Ints; |
| 1444 | } |
| 1445 | |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1446 | /// getValueAsListOfStrings - This method looks up the specified field and |
| 1447 | /// returns its value as a vector of strings, throwing an exception if the |
| 1448 | /// field does not exist or if the value is not the right type. |
| 1449 | /// |
| 1450 | std::vector<std::string> |
| 1451 | Record::getValueAsListOfStrings(StringRef FieldName) const { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1452 | ListInit *List = getValueAsListInit(FieldName); |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1453 | std::vector<std::string> Strings; |
| 1454 | for (unsigned i = 0; i < List->getSize(); i++) { |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1455 | if (StringInit *II = dynamic_cast<StringInit*>(List->getElement(i))) { |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1456 | Strings.push_back(II->getValue()); |
| 1457 | } else { |
| 1458 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
| 1459 | "' does not have a list of strings initializer!"; |
| 1460 | } |
| 1461 | } |
| 1462 | return Strings; |
| 1463 | } |
| 1464 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1465 | /// getValueAsDef - This method looks up the specified field and returns its |
| 1466 | /// value as a Record, throwing an exception if the field does not exist or if |
| 1467 | /// the value is not the right type. |
| 1468 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1469 | Record *Record::getValueAsDef(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1470 | const RecordVal *R = getValue(FieldName); |
| 1471 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1472 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1473 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1474 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1475 | if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1476 | return DI->getDef(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1477 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Nate Begeman | f621b33 | 2005-11-30 18:37:14 +0000 | [diff] [blame] | 1478 | "' does not have a def initializer!"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /// getValueAsBit - This method looks up the specified field and returns its |
| 1482 | /// value as a bit, throwing an exception if the field does not exist or if |
| 1483 | /// the value is not the right type. |
| 1484 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1485 | bool Record::getValueAsBit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1486 | const RecordVal *R = getValue(FieldName); |
| 1487 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1488 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1489 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1490 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1491 | if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1492 | return BI->getValue(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1493 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1494 | "' does not have a bit initializer!"; |
| 1495 | } |
| 1496 | |
| 1497 | /// getValueAsDag - This method looks up the specified field and returns its |
| 1498 | /// value as an Dag, throwing an exception if the field does not exist or if |
| 1499 | /// the value is not the right type. |
| 1500 | /// |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1501 | DagInit *Record::getValueAsDag(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1502 | const RecordVal *R = getValue(FieldName); |
| 1503 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1504 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1505 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1506 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1507 | if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1508 | return DI; |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1509 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1510 | "' does not have a dag initializer!"; |
| 1511 | } |
| 1512 | |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1513 | std::string Record::getValueAsCode(StringRef FieldName) const { |
Chris Lattner | ae939eb | 2005-09-13 21:44:28 +0000 | [diff] [blame] | 1514 | const RecordVal *R = getValue(FieldName); |
| 1515 | if (R == 0 || R->getValue() == 0) |
| 1516 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1517 | FieldName.str() + "'!\n"; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1518 | |
Chris Lattner | ae939eb | 2005-09-13 21:44:28 +0000 | [diff] [blame] | 1519 | if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue())) |
| 1520 | return CI->getValue(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1521 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | ae939eb | 2005-09-13 21:44:28 +0000 | [diff] [blame] | 1522 | "' does not have a code initializer!"; |
| 1523 | } |
| 1524 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1525 | |
David Greene | 7049e79 | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1526 | void MultiClass::dump() const { |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1527 | errs() << "Record:\n"; |
David Greene | 7049e79 | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1528 | Rec.dump(); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1529 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1530 | errs() << "Defs:\n"; |
David Greene | 7049e79 | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1531 | for (RecordVector::const_iterator r = DefPrototypes.begin(), |
| 1532 | rend = DefPrototypes.end(); |
| 1533 | r != rend; |
| 1534 | ++r) { |
| 1535 | (*r)->dump(); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1540 | void RecordKeeper::dump() const { errs() << *this; } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1541 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1542 | raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1543 | OS << "------------- Classes -----------------\n"; |
| 1544 | const std::map<std::string, Record*> &Classes = RK.getClasses(); |
| 1545 | for (std::map<std::string, Record*>::const_iterator I = Classes.begin(), |
Jeff Cohen | 88e7b72 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 1546 | E = Classes.end(); I != E; ++I) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1547 | OS << "class " << *I->second; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1549 | OS << "------------- Defs -----------------\n"; |
| 1550 | const std::map<std::string, Record*> &Defs = RK.getDefs(); |
| 1551 | for (std::map<std::string, Record*>::const_iterator I = Defs.begin(), |
Jeff Cohen | 88e7b72 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 1552 | E = Defs.end(); I != E; ++I) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1553 | OS << "def " << *I->second; |
| 1554 | return OS; |
| 1555 | } |
| 1556 | |
| 1557 | |
| 1558 | /// getAllDerivedDefinitions - This method returns all concrete definitions |
| 1559 | /// that derive from the specified class name. If a class with the specified |
| 1560 | /// name does not exist, an error is printed and true is returned. |
| 1561 | std::vector<Record*> |
| 1562 | RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const { |
Chris Lattner | 9704907 | 2010-12-13 00:20:52 +0000 | [diff] [blame] | 1563 | Record *Class = getClass(ClassName); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1564 | if (!Class) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1565 | throw "ERROR: Couldn't find the `" + ClassName + "' class!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1566 | |
| 1567 | std::vector<Record*> Defs; |
| 1568 | for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(), |
| 1569 | E = getDefs().end(); I != E; ++I) |
| 1570 | if (I->second->isSubClassOf(Class)) |
| 1571 | Defs.push_back(I->second); |
| 1572 | |
| 1573 | return Defs; |
| 1574 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1575 | |