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