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 | |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 14 | #include "llvm/TableGen/Record.h" |
| 15 | #include "llvm/TableGen/Error.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/DataTypes.h" |
David Greene | 50c0912 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
David Greene | dc7b96e | 2011-07-29 19:07:11 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | 08a47fd | 2012-03-05 10:36:16 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Hashing.h" |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
David Greene | 3ff33c9 | 2011-07-29 19:07:14 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringMap.h" |
Duraid Madina | 14492af | 2005-12-26 05:08:55 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 6847866 | 2004-08-01 03:55:39 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 28 | |
| 29 | //===----------------------------------------------------------------------===// |
David Greene | ea844f0 | 2011-07-29 19:06:58 +0000 | [diff] [blame] | 30 | // std::string wrapper for DenseMap purposes |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chandler Carruth | 08a47fd | 2012-03-05 10:36:16 +0000 | [diff] [blame] | 33 | namespace llvm { |
| 34 | |
David Greene | ea844f0 | 2011-07-29 19:06:58 +0000 | [diff] [blame] | 35 | /// TableGenStringKey - This is a wrapper for std::string suitable for |
| 36 | /// using as a key to a DenseMap. Because there isn't a particularly |
| 37 | /// good way to indicate tombstone or empty keys for strings, we want |
| 38 | /// to wrap std::string to indicate that this is a "special" string |
| 39 | /// not expected to take on certain values (those of the tombstone and |
| 40 | /// empty keys). This makes things a little safer as it clarifies |
| 41 | /// that DenseMap is really not appropriate for general strings. |
| 42 | |
| 43 | class TableGenStringKey { |
| 44 | public: |
| 45 | TableGenStringKey(const std::string &str) : data(str) {} |
| 46 | TableGenStringKey(const char *str) : data(str) {} |
| 47 | |
| 48 | const std::string &str() const { return data; } |
Chandler Carruth | 08a47fd | 2012-03-05 10:36:16 +0000 | [diff] [blame] | 49 | |
| 50 | friend hash_code hash_value(const TableGenStringKey &Value) { |
| 51 | using llvm::hash_value; |
| 52 | return hash_value(Value.str()); |
| 53 | } |
David Greene | ea844f0 | 2011-07-29 19:06:58 +0000 | [diff] [blame] | 54 | private: |
| 55 | std::string data; |
| 56 | }; |
| 57 | |
| 58 | /// Specialize DenseMapInfo for TableGenStringKey. |
David Greene | ea844f0 | 2011-07-29 19:06:58 +0000 | [diff] [blame] | 59 | template<> struct DenseMapInfo<TableGenStringKey> { |
| 60 | static inline TableGenStringKey getEmptyKey() { |
| 61 | TableGenStringKey Empty("<<<EMPTY KEY>>>"); |
| 62 | return Empty; |
| 63 | } |
| 64 | static inline TableGenStringKey getTombstoneKey() { |
| 65 | TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>"); |
| 66 | return Tombstone; |
| 67 | } |
| 68 | static unsigned getHashValue(const TableGenStringKey& Val) { |
Chandler Carruth | 08a47fd | 2012-03-05 10:36:16 +0000 | [diff] [blame] | 69 | using llvm::hash_value; |
| 70 | return hash_value(Val); |
David Greene | ea844f0 | 2011-07-29 19:06:58 +0000 | [diff] [blame] | 71 | } |
| 72 | static bool isEqual(const TableGenStringKey& LHS, |
| 73 | const TableGenStringKey& RHS) { |
| 74 | return LHS.str() == RHS.str(); |
| 75 | } |
| 76 | }; |
| 77 | |
Chandler Carruth | 08a47fd | 2012-03-05 10:36:16 +0000 | [diff] [blame] | 78 | } // namespace llvm |
David Greene | ea844f0 | 2011-07-29 19:06:58 +0000 | [diff] [blame] | 79 | |
| 80 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 81 | // Type implementations |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 84 | BitRecTy BitRecTy::Shared; |
| 85 | IntRecTy IntRecTy::Shared; |
| 86 | StringRecTy StringRecTy::Shared; |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 87 | DagRecTy DagRecTy::Shared; |
| 88 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 89 | void RecTy::anchor() { } |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 90 | void RecTy::dump() const { print(errs()); } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 91 | |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 92 | ListRecTy *RecTy::getListTy() { |
| 93 | if (!ListTy) |
| 94 | ListTy = new ListRecTy(this); |
| 95 | return ListTy; |
| 96 | } |
| 97 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 98 | Init *BitRecTy::convertValue(BitsInit *BI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 99 | if (BI->getNumBits() != 1) return 0; // Only accept if just one bit! |
| 100 | return BI->getBit(0); |
| 101 | } |
| 102 | |
| 103 | bool BitRecTy::baseClassOf(const BitsRecTy *RHS) const { |
| 104 | return RHS->getNumBits() == 1; |
| 105 | } |
| 106 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 107 | Init *BitRecTy::convertValue(IntInit *II) { |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 108 | int64_t Val = II->getValue(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 109 | 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] | 110 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 111 | return BitInit::get(Val != 0); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 112 | } |
| 113 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 114 | Init *BitRecTy::convertValue(TypedInit *VI) { |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 115 | RecTy *Ty = VI->getType(); |
| 116 | if (dynamic_cast<BitRecTy*>(Ty) || |
| 117 | dynamic_cast<BitsRecTy*>(Ty) || |
| 118 | dynamic_cast<IntRecTy*>(Ty)) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 119 | return VI; // Accept variable if it is already of bit type! |
| 120 | return 0; |
| 121 | } |
| 122 | |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 123 | BitsRecTy *BitsRecTy::get(unsigned Sz) { |
| 124 | static std::vector<BitsRecTy*> Shared; |
| 125 | if (Sz >= Shared.size()) |
| 126 | Shared.resize(Sz + 1); |
| 127 | BitsRecTy *&Ty = Shared[Sz]; |
| 128 | if (!Ty) |
| 129 | Ty = new BitsRecTy(Sz); |
| 130 | return Ty; |
| 131 | } |
| 132 | |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 133 | std::string BitsRecTy::getAsString() const { |
| 134 | return "bits<" + utostr(Size) + ">"; |
| 135 | } |
| 136 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 137 | Init *BitsRecTy::convertValue(UnsetInit *UI) { |
| 138 | SmallVector<Init *, 16> NewBits(Size); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 139 | |
| 140 | for (unsigned i = 0; i != Size; ++i) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 141 | NewBits[i] = UnsetInit::get(); |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 142 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 143 | return BitsInit::get(NewBits); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 144 | } |
| 145 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 146 | Init *BitsRecTy::convertValue(BitInit *UI) { |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 147 | if (Size != 1) return 0; // Can only convert single bit. |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 148 | return BitsInit::get(UI); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 151 | /// canFitInBitfield - Return true if the number of bits is large enough to hold |
| 152 | /// the integer value. |
| 153 | static bool canFitInBitfield(int64_t Value, unsigned NumBits) { |
Nick Lewycky | 79286bf | 2011-06-03 08:25:39 +0000 | [diff] [blame] | 154 | // For example, with NumBits == 4, we permit Values from [-7 .. 15]. |
| 155 | return (NumBits >= sizeof(Value) * 8) || |
| 156 | (Value >> NumBits == 0) || (Value >> (NumBits-1) == -1); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 159 | /// convertValue from Int initializer to bits type: Split the integer up into the |
| 160 | /// appropriate bits. |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 161 | /// |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 162 | Init *BitsRecTy::convertValue(IntInit *II) { |
Misha Brukman | 6752fb5 | 2004-06-21 18:01:47 +0000 | [diff] [blame] | 163 | int64_t Value = II->getValue(); |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 164 | // Make sure this bitfield is large enough to hold the integer value. |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 165 | if (!canFitInBitfield(Value, Size)) |
| 166 | return 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 167 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 168 | SmallVector<Init *, 16> NewBits(Size); |
David Greene | af973b4 | 2011-07-11 18:25:51 +0000 | [diff] [blame] | 169 | |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 170 | for (unsigned i = 0; i != Size; ++i) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 171 | NewBits[i] = BitInit::get(Value & (1LL << i)); |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 172 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 173 | return BitsInit::get(NewBits); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 174 | } |
| 175 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 176 | Init *BitsRecTy::convertValue(BitsInit *BI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 177 | // 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] | 178 | // truncate. |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 179 | if (BI->getNumBits() == Size) return BI; |
| 180 | return 0; |
| 181 | } |
| 182 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 183 | Init *BitsRecTy::convertValue(TypedInit *VI) { |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 184 | if (Size == 1 && dynamic_cast<BitRecTy*>(VI->getType())) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 185 | return BitsInit::get(VI); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 186 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 187 | if (VI->getType()->typeIsConvertibleTo(this)) { |
| 188 | SmallVector<Init *, 16> NewBits(Size); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 189 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 190 | for (unsigned i = 0; i != Size; ++i) |
| 191 | NewBits[i] = VarBitInit::get(VI, i); |
| 192 | return BitsInit::get(NewBits); |
Bill Wendling | 73ce4a6 | 2010-12-13 01:46:19 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 198 | Init *IntRecTy::convertValue(BitInit *BI) { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 199 | return IntInit::get(BI->getValue()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 200 | } |
| 201 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 202 | Init *IntRecTy::convertValue(BitsInit *BI) { |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 203 | int64_t Result = 0; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 204 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 205 | if (BitInit *Bit = dynamic_cast<BitInit*>(BI->getBit(i))) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 206 | Result |= Bit->getValue() << i; |
| 207 | } else { |
| 208 | return 0; |
| 209 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 210 | return IntInit::get(Result); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 211 | } |
| 212 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 213 | Init *IntRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 214 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 215 | return TI; // Accept variable if already of the right type! |
| 216 | return 0; |
| 217 | } |
| 218 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 219 | Init *StringRecTy::convertValue(UnOpInit *BO) { |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 220 | if (BO->getOpcode() == UnOpInit::CAST) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 221 | Init *L = BO->getOperand()->convertInitializerTo(this); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 222 | if (L == 0) return 0; |
| 223 | if (L != BO->getOperand()) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 224 | return UnOpInit::get(UnOpInit::CAST, L, new StringRecTy); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 225 | return BO; |
| 226 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 227 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 228 | return convertValue((TypedInit*)BO); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 229 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 230 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 231 | Init *StringRecTy::convertValue(BinOpInit *BO) { |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 232 | if (BO->getOpcode() == BinOpInit::STRCONCAT) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 233 | Init *L = BO->getLHS()->convertInitializerTo(this); |
| 234 | Init *R = BO->getRHS()->convertInitializerTo(this); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 235 | if (L == 0 || R == 0) return 0; |
| 236 | if (L != BO->getLHS() || R != BO->getRHS()) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 237 | return BinOpInit::get(BinOpInit::STRCONCAT, L, R, new StringRecTy); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 238 | return BO; |
| 239 | } |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 240 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 241 | return convertValue((TypedInit*)BO); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 245 | Init *StringRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 246 | if (dynamic_cast<StringRecTy*>(TI->getType())) |
| 247 | return TI; // Accept variable if already of the right type! |
| 248 | return 0; |
| 249 | } |
| 250 | |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 251 | std::string ListRecTy::getAsString() const { |
| 252 | return "list<" + Ty->getAsString() + ">"; |
| 253 | } |
| 254 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 255 | Init *ListRecTy::convertValue(ListInit *LI) { |
| 256 | std::vector<Init*> Elements; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 257 | |
| 258 | // Verify that all of the elements of the list are subclasses of the |
| 259 | // appropriate class! |
| 260 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 261 | if (Init *CI = LI->getElement(i)->convertInitializerTo(Ty)) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 262 | Elements.push_back(CI); |
| 263 | else |
| 264 | return 0; |
| 265 | |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 266 | ListRecTy *LType = dynamic_cast<ListRecTy*>(LI->getType()); |
| 267 | if (LType == 0) { |
| 268 | return 0; |
| 269 | } |
| 270 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 271 | return ListInit::get(Elements, this); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 272 | } |
| 273 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 274 | Init *ListRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 275 | // Ensure that TI is compatible with our class. |
| 276 | if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType())) |
| 277 | if (LRT->getElementType()->typeIsConvertibleTo(getElementType())) |
| 278 | return TI; |
| 279 | return 0; |
| 280 | } |
| 281 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 282 | Init *DagRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 283 | if (TI->getType()->typeIsConvertibleTo(this)) |
| 284 | return TI; |
| 285 | return 0; |
| 286 | } |
| 287 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 288 | Init *DagRecTy::convertValue(UnOpInit *BO) { |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 289 | if (BO->getOpcode() == UnOpInit::CAST) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 290 | Init *L = BO->getOperand()->convertInitializerTo(this); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 291 | if (L == 0) return 0; |
| 292 | if (L != BO->getOperand()) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 293 | return UnOpInit::get(UnOpInit::CAST, L, new DagRecTy); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 294 | return BO; |
| 295 | } |
| 296 | return 0; |
| 297 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 298 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 299 | Init *DagRecTy::convertValue(BinOpInit *BO) { |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 300 | if (BO->getOpcode() == BinOpInit::CONCAT) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 301 | Init *L = BO->getLHS()->convertInitializerTo(this); |
| 302 | Init *R = BO->getRHS()->convertInitializerTo(this); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 303 | if (L == 0 || R == 0) return 0; |
| 304 | if (L != BO->getLHS() || R != BO->getRHS()) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 305 | return BinOpInit::get(BinOpInit::CONCAT, L, R, new DagRecTy); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 306 | return BO; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 311 | RecordRecTy *RecordRecTy::get(Record *R) { |
| 312 | return &dynamic_cast<RecordRecTy&>(*R->getDefInit()->getType()); |
| 313 | } |
| 314 | |
Chris Lattner | 8b9ecda | 2007-11-20 22:25:16 +0000 | [diff] [blame] | 315 | std::string RecordRecTy::getAsString() const { |
| 316 | return Rec->getName(); |
| 317 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 318 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 319 | Init *RecordRecTy::convertValue(DefInit *DI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 320 | // Ensure that DI is a subclass of Rec. |
| 321 | if (!DI->getDef()->isSubClassOf(Rec)) |
| 322 | return 0; |
| 323 | return DI; |
| 324 | } |
| 325 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 326 | Init *RecordRecTy::convertValue(TypedInit *TI) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 327 | // Ensure that TI is compatible with Rec. |
| 328 | if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType())) |
| 329 | if (RRT->getRecord()->isSubClassOf(getRecord()) || |
| 330 | RRT->getRecord() == getRecord()) |
| 331 | return TI; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | bool RecordRecTy::baseClassOf(const RecordRecTy *RHS) const { |
Bruno Cardoso Lopes | deb2002 | 2010-06-17 23:00:16 +0000 | [diff] [blame] | 336 | if (Rec == RHS->getRecord() || RHS->getRecord()->isSubClassOf(Rec)) |
| 337 | return true; |
| 338 | |
| 339 | const std::vector<Record*> &SC = Rec->getSuperClasses(); |
| 340 | for (unsigned i = 0, e = SC.size(); i != e; ++i) |
| 341 | if (RHS->getRecord()->isSubClassOf(SC[i])) |
| 342 | return true; |
| 343 | |
| 344 | return false; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 347 | /// resolveTypes - Find a common type that T1 and T2 convert to. |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 348 | /// Return 0 if no such type exists. |
| 349 | /// |
| 350 | RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { |
Sean Silva | 8f43c6f | 2012-09-19 02:14:59 +0000 | [diff] [blame^] | 351 | if (T1->typeIsConvertibleTo(T2)) |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 352 | return T2; |
Sean Silva | 8f43c6f | 2012-09-19 02:14:59 +0000 | [diff] [blame^] | 353 | if (T2->typeIsConvertibleTo(T1)) |
| 354 | return T1; |
| 355 | |
| 356 | // If one is a Record type, check superclasses |
| 357 | if (RecordRecTy *RecTy1 = dynamic_cast<RecordRecTy*>(T1)) { |
| 358 | // See if T2 inherits from a type T1 also inherits from |
| 359 | const std::vector<Record *> &T1SuperClasses = |
| 360 | RecTy1->getRecord()->getSuperClasses(); |
| 361 | for(std::vector<Record *>::const_iterator i = T1SuperClasses.begin(), |
| 362 | iend = T1SuperClasses.end(); |
| 363 | i != iend; |
| 364 | ++i) { |
| 365 | RecordRecTy *SuperRecTy1 = RecordRecTy::get(*i); |
| 366 | RecTy *NewType1 = resolveTypes(SuperRecTy1, T2); |
| 367 | if (NewType1 != 0) { |
| 368 | if (NewType1 != SuperRecTy1) { |
| 369 | delete SuperRecTy1; |
| 370 | } |
| 371 | return NewType1; |
| 372 | } |
| 373 | } |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 374 | } |
Sean Silva | 8f43c6f | 2012-09-19 02:14:59 +0000 | [diff] [blame^] | 375 | if (RecordRecTy *RecTy2 = dynamic_cast<RecordRecTy*>(T2)) { |
| 376 | // See if T1 inherits from a type T2 also inherits from |
| 377 | const std::vector<Record *> &T2SuperClasses = |
| 378 | RecTy2->getRecord()->getSuperClasses(); |
| 379 | for (std::vector<Record *>::const_iterator i = T2SuperClasses.begin(), |
| 380 | iend = T2SuperClasses.end(); |
| 381 | i != iend; |
| 382 | ++i) { |
| 383 | RecordRecTy *SuperRecTy2 = RecordRecTy::get(*i); |
| 384 | RecTy *NewType2 = resolveTypes(T1, SuperRecTy2); |
| 385 | if (NewType2 != 0) { |
| 386 | if (NewType2 != SuperRecTy2) { |
| 387 | delete SuperRecTy2; |
| 388 | } |
| 389 | return NewType2; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | return 0; |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 397 | //===----------------------------------------------------------------------===// |
| 398 | // Initializer implementations |
| 399 | //===----------------------------------------------------------------------===// |
| 400 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 401 | void Init::anchor() { } |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 402 | void Init::dump() const { return print(errs()); } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 403 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 404 | void UnsetInit::anchor() { } |
| 405 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 406 | UnsetInit *UnsetInit::get() { |
| 407 | static UnsetInit TheInit; |
David Greene | 377e12c | 2011-07-29 19:07:09 +0000 | [diff] [blame] | 408 | return &TheInit; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 409 | } |
| 410 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 411 | void BitInit::anchor() { } |
| 412 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 413 | BitInit *BitInit::get(bool V) { |
| 414 | static BitInit True(true); |
| 415 | static BitInit False(false); |
David Greene | 772b2c6 | 2011-07-29 19:07:10 +0000 | [diff] [blame] | 416 | |
| 417 | return V ? &True : &False; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 418 | } |
| 419 | |
David Greene | dc7b96e | 2011-07-29 19:07:11 +0000 | [diff] [blame] | 420 | static void |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 421 | ProfileBitsInit(FoldingSetNodeID &ID, ArrayRef<Init *> Range) { |
David Greene | dc7b96e | 2011-07-29 19:07:11 +0000 | [diff] [blame] | 422 | ID.AddInteger(Range.size()); |
| 423 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 424 | for (ArrayRef<Init *>::iterator i = Range.begin(), |
David Greene | dc7b96e | 2011-07-29 19:07:11 +0000 | [diff] [blame] | 425 | iend = Range.end(); |
| 426 | i != iend; |
| 427 | ++i) |
| 428 | ID.AddPointer(*i); |
| 429 | } |
| 430 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 431 | BitsInit *BitsInit::get(ArrayRef<Init *> Range) { |
David Greene | dc7b96e | 2011-07-29 19:07:11 +0000 | [diff] [blame] | 432 | typedef FoldingSet<BitsInit> Pool; |
| 433 | static Pool ThePool; |
| 434 | |
| 435 | FoldingSetNodeID ID; |
| 436 | ProfileBitsInit(ID, Range); |
| 437 | |
| 438 | void *IP = 0; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 439 | if (BitsInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) |
David Greene | dc7b96e | 2011-07-29 19:07:11 +0000 | [diff] [blame] | 440 | return I; |
| 441 | |
| 442 | BitsInit *I = new BitsInit(Range); |
| 443 | ThePool.InsertNode(I, IP); |
| 444 | |
| 445 | return I; |
| 446 | } |
| 447 | |
| 448 | void BitsInit::Profile(FoldingSetNodeID &ID) const { |
| 449 | ProfileBitsInit(ID, Bits); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 450 | } |
| 451 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 452 | Init * |
David Greene | 1aa0e3e | 2011-07-29 19:07:05 +0000 | [diff] [blame] | 453 | BitsInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 454 | SmallVector<Init *, 16> NewBits(Bits.size()); |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 455 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 456 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 457 | if (Bits[i] >= getNumBits()) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 458 | return 0; |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 459 | NewBits[i] = getBit(Bits[i]); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 460 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 461 | return BitsInit::get(NewBits); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 464 | std::string BitsInit::getAsString() const { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 465 | std::string Result = "{ "; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 466 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 467 | if (i) Result += ", "; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 468 | if (Init *Bit = getBit(e-i-1)) |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 469 | Result += Bit->getAsString(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 470 | else |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 471 | Result += "*"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 472 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 473 | return Result + " }"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 476 | // Fix bit initializer to preserve the behavior that bit reference from a unset |
| 477 | // bits initializer will resolve into VarBitInit to keep the field name and bit |
| 478 | // number used in targets with fixed insn length. |
| 479 | static Init *fixBitInit(const RecordVal *RV, Init *Before, Init *After) { |
| 480 | if (RV || After != UnsetInit::get()) |
| 481 | return After; |
| 482 | return Before; |
| 483 | } |
| 484 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 485 | // resolveReferences - If there are any field references that refer to fields |
| 486 | // that have been filled in, we can propagate the values now. |
| 487 | // |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 488 | Init *BitsInit::resolveReferences(Record &R, const RecordVal *RV) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 489 | bool Changed = false; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 490 | SmallVector<Init *, 16> NewBits(getNumBits()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 491 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 492 | Init *CachedInit = 0; |
| 493 | Init *CachedBitVar = 0; |
| 494 | bool CachedBitVarChanged = false; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 495 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 496 | for (unsigned i = 0, e = getNumBits(); i != e; ++i) { |
| 497 | Init *CurBit = Bits[i]; |
| 498 | Init *CurBitVar = CurBit->getBitVar(); |
| 499 | |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 500 | NewBits[i] = CurBit; |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 501 | |
| 502 | if (CurBitVar == CachedBitVar) { |
| 503 | if (CachedBitVarChanged) { |
| 504 | Init *Bit = CachedInit->getBit(CurBit->getBitNum()); |
| 505 | NewBits[i] = fixBitInit(RV, CurBit, Bit); |
| 506 | } |
| 507 | continue; |
| 508 | } |
| 509 | CachedBitVar = CurBitVar; |
| 510 | CachedBitVarChanged = false; |
| 511 | |
| 512 | Init *B; |
| 513 | do { |
| 514 | B = CurBitVar; |
| 515 | CurBitVar = CurBitVar->resolveReferences(R, RV); |
| 516 | CachedBitVarChanged |= B != CurBitVar; |
| 517 | Changed |= B != CurBitVar; |
| 518 | } while (B != CurBitVar); |
| 519 | CachedInit = CurBitVar; |
| 520 | |
| 521 | if (CachedBitVarChanged) { |
| 522 | Init *Bit = CurBitVar->getBit(CurBit->getBitNum()); |
| 523 | NewBits[i] = fixBitInit(RV, CurBit, Bit); |
| 524 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | if (Changed) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 528 | return BitsInit::get(NewBits); |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 529 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 530 | return const_cast<BitsInit *>(this); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 531 | } |
| 532 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 533 | IntInit *IntInit::get(int64_t V) { |
David Greene | a44263c | 2011-07-29 19:07:12 +0000 | [diff] [blame] | 534 | typedef DenseMap<int64_t, IntInit *> Pool; |
| 535 | static Pool ThePool; |
| 536 | |
| 537 | IntInit *&I = ThePool[V]; |
| 538 | if (!I) I = new IntInit(V); |
| 539 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 542 | std::string IntInit::getAsString() const { |
| 543 | return itostr(Value); |
| 544 | } |
| 545 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 546 | Init * |
David Greene | 1aa0e3e | 2011-07-29 19:07:05 +0000 | [diff] [blame] | 547 | IntInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 548 | SmallVector<Init *, 16> NewBits(Bits.size()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 549 | |
| 550 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 551 | if (Bits[i] >= 64) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 552 | return 0; |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 553 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 554 | NewBits[i] = BitInit::get(Value & (INT64_C(1) << Bits[i])); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 555 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 556 | return BitsInit::get(NewBits); |
| 557 | } |
| 558 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 559 | void StringInit::anchor() { } |
| 560 | |
Jakob Stoklund Olesen | 9d1c5ee | 2012-01-13 03:16:35 +0000 | [diff] [blame] | 561 | StringInit *StringInit::get(StringRef V) { |
David Greene | 3ff33c9 | 2011-07-29 19:07:14 +0000 | [diff] [blame] | 562 | typedef StringMap<StringInit *> Pool; |
| 563 | static Pool ThePool; |
| 564 | |
| 565 | StringInit *&I = ThePool[V]; |
| 566 | if (!I) I = new StringInit(V); |
| 567 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 568 | } |
| 569 | |
David Greene | c52270b | 2011-07-29 19:07:16 +0000 | [diff] [blame] | 570 | static void ProfileListInit(FoldingSetNodeID &ID, |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 571 | ArrayRef<Init *> Range, |
David Greene | c52270b | 2011-07-29 19:07:16 +0000 | [diff] [blame] | 572 | RecTy *EltTy) { |
| 573 | ID.AddInteger(Range.size()); |
| 574 | ID.AddPointer(EltTy); |
| 575 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 576 | for (ArrayRef<Init *>::iterator i = Range.begin(), |
David Greene | c52270b | 2011-07-29 19:07:16 +0000 | [diff] [blame] | 577 | iend = Range.end(); |
| 578 | i != iend; |
| 579 | ++i) |
| 580 | ID.AddPointer(*i); |
| 581 | } |
| 582 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 583 | ListInit *ListInit::get(ArrayRef<Init *> Range, RecTy *EltTy) { |
David Greene | c52270b | 2011-07-29 19:07:16 +0000 | [diff] [blame] | 584 | typedef FoldingSet<ListInit> Pool; |
| 585 | static Pool ThePool; |
| 586 | |
| 587 | // Just use the FoldingSetNodeID to compute a hash. Use a DenseMap |
| 588 | // for actual storage. |
| 589 | FoldingSetNodeID ID; |
| 590 | ProfileListInit(ID, Range, EltTy); |
| 591 | |
| 592 | void *IP = 0; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 593 | if (ListInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) |
David Greene | c52270b | 2011-07-29 19:07:16 +0000 | [diff] [blame] | 594 | return I; |
| 595 | |
| 596 | ListInit *I = new ListInit(Range, EltTy); |
| 597 | ThePool.InsertNode(I, IP); |
| 598 | return I; |
| 599 | } |
| 600 | |
| 601 | void ListInit::Profile(FoldingSetNodeID &ID) const { |
| 602 | ListRecTy *ListType = dynamic_cast<ListRecTy *>(getType()); |
| 603 | assert(ListType && "Bad type for ListInit!"); |
| 604 | RecTy *EltTy = ListType->getElementType(); |
| 605 | |
| 606 | ProfileListInit(ID, Values, EltTy); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 607 | } |
| 608 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 609 | Init * |
David Greene | 1aa0e3e | 2011-07-29 19:07:05 +0000 | [diff] [blame] | 610 | ListInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 611 | std::vector<Init*> Vals; |
Chris Lattner | 8bf9e06 | 2004-07-26 23:21:34 +0000 | [diff] [blame] | 612 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) { |
| 613 | if (Elements[i] >= getSize()) |
| 614 | return 0; |
| 615 | Vals.push_back(getElement(Elements[i])); |
| 616 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 617 | return ListInit::get(Vals, getType()); |
Chris Lattner | 8bf9e06 | 2004-07-26 23:21:34 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Chris Lattner | cbebe46 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 620 | Record *ListInit::getElementAsRecord(unsigned i) const { |
| 621 | assert(i < Values.size() && "List element index out of range!"); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 622 | DefInit *DI = dynamic_cast<DefInit*>(Values[i]); |
Chris Lattner | cbebe46 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 623 | if (DI == 0) throw "Expected record in list!"; |
| 624 | return DI->getDef(); |
| 625 | } |
| 626 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 627 | Init *ListInit::resolveReferences(Record &R, const RecordVal *RV) const { |
| 628 | std::vector<Init*> Resolved; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 629 | Resolved.reserve(getSize()); |
| 630 | bool Changed = false; |
| 631 | |
| 632 | for (unsigned i = 0, e = getSize(); i != e; ++i) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 633 | Init *E; |
| 634 | Init *CurElt = getElement(i); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 635 | |
| 636 | do { |
| 637 | E = CurElt; |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 638 | CurElt = CurElt->resolveReferences(R, RV); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 639 | Changed |= E != CurElt; |
| 640 | } while (E != CurElt); |
| 641 | Resolved.push_back(E); |
| 642 | } |
| 643 | |
| 644 | if (Changed) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 645 | return ListInit::get(Resolved, getType()); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 646 | return const_cast<ListInit *>(this); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 647 | } |
| 648 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 649 | Init *ListInit::resolveListElementReference(Record &R, const RecordVal *IRV, |
| 650 | unsigned Elt) const { |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 651 | if (Elt >= getSize()) |
| 652 | return 0; // Out of range reference. |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 653 | Init *E = getElement(Elt); |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 654 | // If the element is set to some value, or if we are resolving a reference |
| 655 | // to a specific variable and that variable is explicitly unset, then |
| 656 | // replace the VarListElementInit with it. |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 657 | if (IRV || !dynamic_cast<UnsetInit*>(E)) |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 658 | return E; |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 659 | return 0; |
| 660 | } |
| 661 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 662 | std::string ListInit::getAsString() const { |
| 663 | std::string Result = "["; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 664 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 665 | if (i) Result += ", "; |
| 666 | Result += Values[i]->getAsString(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 667 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 668 | return Result + "]"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 669 | } |
| 670 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 671 | Init *OpInit::resolveListElementReference(Record &R, const RecordVal *IRV, |
| 672 | unsigned Elt) const { |
David Greene | a8b8ab6 | 2011-10-04 18:55:36 +0000 | [diff] [blame] | 673 | Init *Resolved = resolveReferences(R, IRV); |
| 674 | OpInit *OResolved = dynamic_cast<OpInit *>(Resolved); |
| 675 | if (OResolved) { |
| 676 | Resolved = OResolved->Fold(&R, 0); |
| 677 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 678 | |
David Greene | a8b8ab6 | 2011-10-04 18:55:36 +0000 | [diff] [blame] | 679 | if (Resolved != this) { |
| 680 | TypedInit *Typed = dynamic_cast<TypedInit *>(Resolved); |
| 681 | assert(Typed && "Expected typed init for list reference"); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 682 | if (Typed) { |
David Greene | a8b8ab6 | 2011-10-04 18:55:36 +0000 | [diff] [blame] | 683 | Init *New = Typed->resolveListElementReference(R, IRV, Elt); |
| 684 | if (New) |
| 685 | return New; |
| 686 | return VarListElementInit::get(Typed, Elt); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 687 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 688 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 689 | |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 690 | return 0; |
| 691 | } |
| 692 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 693 | Init *OpInit::getBit(unsigned Bit) const { |
| 694 | if (getType() == BitRecTy::get()) |
| 695 | return const_cast<OpInit*>(this); |
| 696 | return VarBitInit::get(const_cast<OpInit*>(this), Bit); |
| 697 | } |
| 698 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 699 | UnOpInit *UnOpInit::get(UnaryOp opc, Init *lhs, RecTy *Type) { |
| 700 | typedef std::pair<std::pair<unsigned, Init *>, RecTy *> Key; |
David Greene | 2b6c8b3 | 2011-07-29 19:07:18 +0000 | [diff] [blame] | 701 | |
| 702 | typedef DenseMap<Key, UnOpInit *> Pool; |
| 703 | static Pool ThePool; |
| 704 | |
| 705 | Key TheKey(std::make_pair(std::make_pair(opc, lhs), Type)); |
| 706 | |
| 707 | UnOpInit *&I = ThePool[TheKey]; |
| 708 | if (!I) I = new UnOpInit(opc, lhs, Type); |
| 709 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 710 | } |
| 711 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 712 | Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 713 | switch (getOpcode()) { |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 714 | case CAST: { |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 715 | if (getType()->getAsString() == "string") { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 716 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 717 | if (LHSs) { |
| 718 | return LHSs; |
| 719 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 720 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 721 | DefInit *LHSd = dynamic_cast<DefInit*>(LHS); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 722 | if (LHSd) { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 723 | return StringInit::get(LHSd->getDef()->getName()); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 724 | } |
David Greene | 6291f3a | 2012-01-30 20:47:04 +0000 | [diff] [blame] | 725 | |
| 726 | IntInit *LHSi = dynamic_cast<IntInit*>(LHS); |
| 727 | if (LHSi) { |
| 728 | return StringInit::get(LHSi->getAsString()); |
| 729 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 730 | } else { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 731 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 732 | if (LHSs) { |
| 733 | std::string Name = LHSs->getValue(); |
| 734 | |
| 735 | // From TGParser::ParseIDValue |
| 736 | if (CurRec) { |
| 737 | if (const RecordVal *RV = CurRec->getValue(Name)) { |
Chris Lattner | 9402633 | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 738 | if (RV->getType() != getType()) |
| 739 | throw "type mismatch in cast"; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 740 | return VarInit::get(Name, RV->getType()); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 741 | } |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 742 | |
David Greene | db10e69 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 743 | Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, |
| 744 | ":"); |
| 745 | |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 746 | if (CurRec->isTemplateArg(TemplateArgName)) { |
| 747 | const RecordVal *RV = CurRec->getValue(TemplateArgName); |
| 748 | assert(RV && "Template arg doesn't exist??"); |
| 749 | |
Chris Lattner | 9402633 | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 750 | if (RV->getType() != getType()) |
| 751 | throw "type mismatch in cast"; |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 752 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 753 | return VarInit::get(TemplateArgName, RV->getType()); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | if (CurMultiClass) { |
David Greene | db10e69 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 758 | Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::"); |
| 759 | |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 760 | if (CurMultiClass->Rec.isTemplateArg(MCName)) { |
| 761 | const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); |
| 762 | assert(RV && "Template arg doesn't exist??"); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 9402633 | 2010-10-06 00:19:21 +0000 | [diff] [blame] | 764 | if (RV->getType() != getType()) |
| 765 | throw "type mismatch in cast"; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 766 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 767 | return VarInit::get(MCName, RV->getType()); |
David Greene | efa1961 | 2009-06-29 20:05:29 +0000 | [diff] [blame] | 768 | } |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 769 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 771 | if (Record *D = (CurRec->getRecords()).getDef(Name)) |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 772 | return DefInit::get(D); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 773 | |
Jim Grosbach | 59ddb73 | 2011-05-06 18:47:45 +0000 | [diff] [blame] | 774 | throw TGError(CurRec->getLoc(), "Undefined reference:'" + Name + "'\n"); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 775 | } |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 776 | } |
| 777 | break; |
| 778 | } |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 779 | case HEAD: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 780 | ListInit *LHSl = dynamic_cast<ListInit*>(LHS); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 781 | if (LHSl) { |
| 782 | if (LHSl->getSize() == 0) { |
| 783 | assert(0 && "Empty list in car"); |
| 784 | return 0; |
| 785 | } |
| 786 | return LHSl->getElement(0); |
| 787 | } |
| 788 | break; |
| 789 | } |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 790 | case TAIL: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 791 | ListInit *LHSl = dynamic_cast<ListInit*>(LHS); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 792 | if (LHSl) { |
| 793 | if (LHSl->getSize() == 0) { |
| 794 | assert(0 && "Empty list in cdr"); |
| 795 | return 0; |
| 796 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 797 | // Note the +1. We can't just pass the result of getValues() |
| 798 | // directly. |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 799 | ArrayRef<Init *>::iterator begin = LHSl->getValues().begin()+1; |
| 800 | ArrayRef<Init *>::iterator end = LHSl->getValues().end(); |
| 801 | ListInit *Result = |
| 802 | ListInit::get(ArrayRef<Init *>(begin, end - begin), |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 803 | LHSl->getType()); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 804 | return Result; |
| 805 | } |
| 806 | break; |
| 807 | } |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 808 | case EMPTY: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 809 | ListInit *LHSl = dynamic_cast<ListInit*>(LHS); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 810 | if (LHSl) { |
| 811 | if (LHSl->getSize() == 0) { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 812 | return IntInit::get(1); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 813 | } else { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 814 | return IntInit::get(0); |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 817 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 818 | if (LHSs) { |
| 819 | if (LHSs->getValue().empty()) { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 820 | return IntInit::get(1); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 821 | } else { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 822 | return IntInit::get(0); |
David Greene | 8618f95 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 825 | |
David Greene | d571b3c | 2009-05-14 22:38:31 +0000 | [diff] [blame] | 826 | break; |
| 827 | } |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 828 | } |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 829 | return const_cast<UnOpInit *>(this); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 830 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 831 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 832 | Init *UnOpInit::resolveReferences(Record &R, const RecordVal *RV) const { |
| 833 | Init *lhs = LHS->resolveReferences(R, RV); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 834 | |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 835 | if (LHS != lhs) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 836 | return (UnOpInit::get(getOpcode(), lhs, getType()))->Fold(&R, 0); |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 837 | return Fold(&R, 0); |
| 838 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 839 | |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 840 | std::string UnOpInit::getAsString() const { |
| 841 | std::string Result; |
| 842 | switch (Opc) { |
| 843 | case CAST: Result = "!cast<" + getType()->getAsString() + ">"; break; |
David Greene | 2f7cf7f | 2011-01-07 17:05:37 +0000 | [diff] [blame] | 844 | case HEAD: Result = "!head"; break; |
| 845 | case TAIL: Result = "!tail"; break; |
| 846 | case EMPTY: Result = "!empty"; break; |
David Greene | e8f3b27 | 2009-05-14 21:22:49 +0000 | [diff] [blame] | 847 | } |
| 848 | return Result + "(" + LHS->getAsString() + ")"; |
| 849 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 850 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 851 | BinOpInit *BinOpInit::get(BinaryOp opc, Init *lhs, |
| 852 | Init *rhs, RecTy *Type) { |
David Greene | 3acab9c | 2011-07-29 19:07:19 +0000 | [diff] [blame] | 853 | typedef std::pair< |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 854 | std::pair<std::pair<unsigned, Init *>, Init *>, |
David Greene | 3acab9c | 2011-07-29 19:07:19 +0000 | [diff] [blame] | 855 | RecTy * |
| 856 | > Key; |
| 857 | |
| 858 | typedef DenseMap<Key, BinOpInit *> Pool; |
| 859 | static Pool ThePool; |
| 860 | |
| 861 | Key TheKey(std::make_pair(std::make_pair(std::make_pair(opc, lhs), rhs), |
| 862 | Type)); |
| 863 | |
| 864 | BinOpInit *&I = ThePool[TheKey]; |
| 865 | if (!I) I = new BinOpInit(opc, lhs, rhs, Type); |
| 866 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 867 | } |
| 868 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 869 | Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 870 | switch (getOpcode()) { |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 871 | case CONCAT: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 872 | DagInit *LHSs = dynamic_cast<DagInit*>(LHS); |
| 873 | DagInit *RHSs = dynamic_cast<DagInit*>(RHS); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 874 | if (LHSs && RHSs) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 875 | DefInit *LOp = dynamic_cast<DefInit*>(LHSs->getOperator()); |
| 876 | DefInit *ROp = dynamic_cast<DefInit*>(RHSs->getOperator()); |
Chris Lattner | 81fd1f2 | 2010-03-18 21:07:51 +0000 | [diff] [blame] | 877 | if (LOp == 0 || ROp == 0 || LOp->getDef() != ROp->getDef()) |
| 878 | throw "Concated Dag operators do not match!"; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 879 | std::vector<Init*> Args; |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 880 | std::vector<std::string> ArgNames; |
| 881 | for (unsigned i = 0, e = LHSs->getNumArgs(); i != e; ++i) { |
| 882 | Args.push_back(LHSs->getArg(i)); |
| 883 | ArgNames.push_back(LHSs->getArgName(i)); |
| 884 | } |
| 885 | for (unsigned i = 0, e = RHSs->getNumArgs(); i != e; ++i) { |
| 886 | Args.push_back(RHSs->getArg(i)); |
| 887 | ArgNames.push_back(RHSs->getArgName(i)); |
| 888 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 889 | return DagInit::get(LHSs->getOperator(), "", Args, ArgNames); |
Evan Cheng | a32dee2 | 2007-05-15 01:23:24 +0000 | [diff] [blame] | 890 | } |
| 891 | break; |
| 892 | } |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 893 | case STRCONCAT: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 894 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
| 895 | StringInit *RHSs = dynamic_cast<StringInit*>(RHS); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 896 | if (LHSs && RHSs) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 897 | return StringInit::get(LHSs->getValue() + RHSs->getValue()); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 898 | break; |
| 899 | } |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 900 | case EQ: { |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 901 | // try to fold eq comparison for 'bit' and 'int', otherwise fallback |
| 902 | // to string objects. |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 903 | IntInit *L = |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 904 | dynamic_cast<IntInit*>(LHS->convertInitializerTo(IntRecTy::get())); |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 905 | IntInit *R = |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 906 | dynamic_cast<IntInit*>(RHS->convertInitializerTo(IntRecTy::get())); |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 907 | |
| 908 | if (L && R) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 909 | return IntInit::get(L->getValue() == R->getValue()); |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 910 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 911 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
| 912 | StringInit *RHSs = dynamic_cast<StringInit*>(RHS); |
Bruno Cardoso Lopes | 77a4a56 | 2010-06-16 23:24:12 +0000 | [diff] [blame] | 913 | |
| 914 | // Make sure we've resolved |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 915 | if (LHSs && RHSs) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 916 | return IntInit::get(LHSs->getValue() == RHSs->getValue()); |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 917 | |
| 918 | break; |
| 919 | } |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 920 | case SHL: |
| 921 | case SRA: |
| 922 | case SRL: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 923 | IntInit *LHSi = dynamic_cast<IntInit*>(LHS); |
| 924 | IntInit *RHSi = dynamic_cast<IntInit*>(RHS); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 925 | if (LHSi && RHSi) { |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 926 | int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue(); |
| 927 | int64_t Result; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 928 | switch (getOpcode()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 929 | default: llvm_unreachable("Bad opcode!"); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 930 | case SHL: Result = LHSv << RHSv; break; |
| 931 | case SRA: Result = LHSv >> RHSv; break; |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 932 | case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 933 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 934 | return IntInit::get(Result); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 935 | } |
| 936 | break; |
| 937 | } |
| 938 | } |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 939 | return const_cast<BinOpInit *>(this); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 940 | } |
| 941 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 942 | Init *BinOpInit::resolveReferences(Record &R, const RecordVal *RV) const { |
| 943 | Init *lhs = LHS->resolveReferences(R, RV); |
| 944 | Init *rhs = RHS->resolveReferences(R, RV); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 945 | |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 946 | if (LHS != lhs || RHS != rhs) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 947 | return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))->Fold(&R, 0); |
David Greene | a9c6c5d | 2009-04-22 20:18:10 +0000 | [diff] [blame] | 948 | return Fold(&R, 0); |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 951 | std::string BinOpInit::getAsString() const { |
| 952 | std::string Result; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 953 | switch (Opc) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 954 | case CONCAT: Result = "!con"; break; |
| 955 | case SHL: Result = "!shl"; break; |
| 956 | case SRA: Result = "!sra"; break; |
| 957 | case SRL: Result = "!srl"; break; |
David Greene | 297bfe6 | 2010-01-05 19:11:42 +0000 | [diff] [blame] | 958 | case EQ: Result = "!eq"; break; |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 959 | case STRCONCAT: Result = "!strconcat"; break; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 960 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 961 | return Result + "(" + LHS->getAsString() + ", " + RHS->getAsString() + ")"; |
Chris Lattner | 51ffbf1 | 2006-03-31 21:53:49 +0000 | [diff] [blame] | 962 | } |
| 963 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 964 | TernOpInit *TernOpInit::get(TernaryOp opc, Init *lhs, |
| 965 | Init *mhs, Init *rhs, |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 966 | RecTy *Type) { |
David Greene | daba488 | 2011-07-29 19:07:20 +0000 | [diff] [blame] | 967 | typedef std::pair< |
| 968 | std::pair< |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 969 | std::pair<std::pair<unsigned, RecTy *>, Init *>, |
| 970 | Init * |
David Greene | daba488 | 2011-07-29 19:07:20 +0000 | [diff] [blame] | 971 | >, |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 972 | Init * |
David Greene | daba488 | 2011-07-29 19:07:20 +0000 | [diff] [blame] | 973 | > Key; |
| 974 | |
| 975 | typedef DenseMap<Key, TernOpInit *> Pool; |
| 976 | static Pool ThePool; |
| 977 | |
| 978 | Key TheKey(std::make_pair(std::make_pair(std::make_pair(std::make_pair(opc, |
| 979 | Type), |
| 980 | lhs), |
| 981 | mhs), |
| 982 | rhs)); |
| 983 | |
| 984 | TernOpInit *&I = ThePool[TheKey]; |
| 985 | if (!I) I = new TernOpInit(opc, lhs, mhs, rhs, Type); |
| 986 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 987 | } |
| 988 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 989 | static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, |
| 990 | Record *CurRec, MultiClass *CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 991 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 992 | static Init *EvaluateOperation(OpInit *RHSo, Init *LHS, Init *Arg, |
| 993 | RecTy *Type, Record *CurRec, |
| 994 | MultiClass *CurMultiClass) { |
| 995 | std::vector<Init *> NewOperands; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 996 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 997 | TypedInit *TArg = dynamic_cast<TypedInit*>(Arg); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 998 | |
| 999 | // If this is a dag, recurse |
| 1000 | if (TArg && TArg->getType()->getAsString() == "dag") { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1001 | Init *Result = ForeachHelper(LHS, Arg, RHSo, Type, |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1002 | CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1003 | if (Result != 0) { |
| 1004 | return Result; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1005 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1006 | return 0; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | for (int i = 0; i < RHSo->getNumOperands(); ++i) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1011 | OpInit *RHSoo = dynamic_cast<OpInit*>(RHSo->getOperand(i)); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1012 | |
| 1013 | if (RHSoo) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1014 | Init *Result = EvaluateOperation(RHSoo, LHS, Arg, |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1015 | Type, CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1016 | if (Result != 0) { |
| 1017 | NewOperands.push_back(Result); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1018 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1019 | NewOperands.push_back(Arg); |
| 1020 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1021 | } else if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1022 | NewOperands.push_back(Arg); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1023 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1024 | NewOperands.push_back(RHSo->getOperand(i)); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | // Now run the operator and use its result as the new leaf |
David Greene | 1aa0e3e | 2011-07-29 19:07:05 +0000 | [diff] [blame] | 1029 | const OpInit *NewOp = RHSo->clone(NewOperands); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1030 | Init *NewVal = NewOp->Fold(CurRec, CurMultiClass); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1031 | if (NewVal != NewOp) |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1032 | return NewVal; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1033 | |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1034 | return 0; |
| 1035 | } |
| 1036 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1037 | static Init *ForeachHelper(Init *LHS, Init *MHS, Init *RHS, RecTy *Type, |
| 1038 | Record *CurRec, MultiClass *CurMultiClass) { |
| 1039 | DagInit *MHSd = dynamic_cast<DagInit*>(MHS); |
| 1040 | ListInit *MHSl = dynamic_cast<ListInit*>(MHS); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1041 | |
| 1042 | DagRecTy *DagType = dynamic_cast<DagRecTy*>(Type); |
| 1043 | ListRecTy *ListType = dynamic_cast<ListRecTy*>(Type); |
| 1044 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1045 | OpInit *RHSo = dynamic_cast<OpInit*>(RHS); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1046 | |
| 1047 | if (!RHSo) { |
Jim Grosbach | 59ddb73 | 2011-05-06 18:47:45 +0000 | [diff] [blame] | 1048 | throw TGError(CurRec->getLoc(), "!foreach requires an operator\n"); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1051 | TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1052 | |
| 1053 | if (!LHSt) { |
Jim Grosbach | 59ddb73 | 2011-05-06 18:47:45 +0000 | [diff] [blame] | 1054 | throw TGError(CurRec->getLoc(), "!foreach requires typed variable\n"); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Nick Lewycky | 9429822 | 2009-05-15 03:07:14 +0000 | [diff] [blame] | 1057 | if ((MHSd && DagType) || (MHSl && ListType)) { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1058 | if (MHSd) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1059 | Init *Val = MHSd->getOperator(); |
| 1060 | Init *Result = EvaluateOperation(RHSo, LHS, Val, |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1061 | Type, CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1062 | if (Result != 0) { |
| 1063 | Val = Result; |
| 1064 | } |
| 1065 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1066 | std::vector<std::pair<Init *, std::string> > args; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1067 | for (unsigned int i = 0; i < MHSd->getNumArgs(); ++i) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1068 | Init *Arg; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1069 | std::string ArgName; |
| 1070 | Arg = MHSd->getArg(i); |
| 1071 | ArgName = MHSd->getArgName(i); |
| 1072 | |
| 1073 | // Process args |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1074 | Init *Result = EvaluateOperation(RHSo, LHS, Arg, Type, |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1075 | CurRec, CurMultiClass); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1076 | if (Result != 0) { |
| 1077 | Arg = Result; |
| 1078 | } |
| 1079 | |
| 1080 | // TODO: Process arg names |
| 1081 | args.push_back(std::make_pair(Arg, ArgName)); |
| 1082 | } |
| 1083 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1084 | return DagInit::get(Val, "", args); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1085 | } |
| 1086 | if (MHSl) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1087 | std::vector<Init *> NewOperands; |
| 1088 | std::vector<Init *> NewList(MHSl->begin(), MHSl->end()); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1089 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1090 | for (std::vector<Init *>::iterator li = NewList.begin(), |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1091 | liend = NewList.end(); |
| 1092 | li != liend; |
| 1093 | ++li) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1094 | Init *Item = *li; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1095 | NewOperands.clear(); |
| 1096 | for(int i = 0; i < RHSo->getNumOperands(); ++i) { |
| 1097 | // First, replace the foreach variable with the list item |
| 1098 | if (LHS->getAsString() == RHSo->getOperand(i)->getAsString()) { |
| 1099 | NewOperands.push_back(Item); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1100 | } else { |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1101 | NewOperands.push_back(RHSo->getOperand(i)); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | // Now run the operator and use its result as the new list item |
David Greene | 1aa0e3e | 2011-07-29 19:07:05 +0000 | [diff] [blame] | 1106 | const OpInit *NewOp = RHSo->clone(NewOperands); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1107 | Init *NewItem = NewOp->Fold(CurRec, CurMultiClass); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1108 | if (NewItem != NewOp) |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1109 | *li = NewItem; |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1110 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1111 | return ListInit::get(NewList, MHSl->getType()); |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1117 | Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) const { |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1118 | switch (getOpcode()) { |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1119 | case SUBST: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1120 | DefInit *LHSd = dynamic_cast<DefInit*>(LHS); |
| 1121 | VarInit *LHSv = dynamic_cast<VarInit*>(LHS); |
| 1122 | StringInit *LHSs = dynamic_cast<StringInit*>(LHS); |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 1123 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1124 | DefInit *MHSd = dynamic_cast<DefInit*>(MHS); |
| 1125 | VarInit *MHSv = dynamic_cast<VarInit*>(MHS); |
| 1126 | StringInit *MHSs = dynamic_cast<StringInit*>(MHS); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1127 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1128 | DefInit *RHSd = dynamic_cast<DefInit*>(RHS); |
| 1129 | VarInit *RHSv = dynamic_cast<VarInit*>(RHS); |
| 1130 | StringInit *RHSs = dynamic_cast<StringInit*>(RHS); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1131 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1132 | if ((LHSd && MHSd && RHSd) |
| 1133 | || (LHSv && MHSv && RHSv) |
| 1134 | || (LHSs && MHSs && RHSs)) { |
| 1135 | if (RHSd) { |
| 1136 | Record *Val = RHSd->getDef(); |
| 1137 | if (LHSd->getAsString() == RHSd->getAsString()) { |
| 1138 | Val = MHSd->getDef(); |
| 1139 | } |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 1140 | return DefInit::get(Val); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1141 | } |
| 1142 | if (RHSv) { |
| 1143 | std::string Val = RHSv->getName(); |
| 1144 | if (LHSv->getAsString() == RHSv->getAsString()) { |
| 1145 | Val = MHSv->getName(); |
| 1146 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1147 | return VarInit::get(Val, getType()); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1148 | } |
| 1149 | if (RHSs) { |
| 1150 | std::string Val = RHSs->getValue(); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1151 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1152 | std::string::size_type found; |
David Greene | dbf7074 | 2009-12-21 21:21:34 +0000 | [diff] [blame] | 1153 | std::string::size_type idx = 0; |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1154 | do { |
David Greene | dbf7074 | 2009-12-21 21:21:34 +0000 | [diff] [blame] | 1155 | found = Val.find(LHSs->getValue(), idx); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1156 | if (found != std::string::npos) { |
| 1157 | Val.replace(found, LHSs->getValue().size(), MHSs->getValue()); |
| 1158 | } |
David Greene | dbf7074 | 2009-12-21 21:21:34 +0000 | [diff] [blame] | 1159 | idx = found + MHSs->getValue().size(); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1160 | } while (found != std::string::npos); |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1161 | |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1162 | return StringInit::get(Val); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | break; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1166 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1167 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1168 | case FOREACH: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1169 | Init *Result = ForeachHelper(LHS, MHS, RHS, getType(), |
David Greene | e917fff | 2009-05-14 22:23:47 +0000 | [diff] [blame] | 1170 | CurRec, CurMultiClass); |
| 1171 | if (Result != 0) { |
| 1172 | return Result; |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1173 | } |
| 1174 | break; |
| 1175 | } |
David Greene | 3587eed | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1176 | |
| 1177 | case IF: { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1178 | IntInit *LHSi = dynamic_cast<IntInit*>(LHS); |
| 1179 | if (Init *I = LHS->convertInitializerTo(IntRecTy::get())) |
| 1180 | LHSi = dynamic_cast<IntInit*>(I); |
David Greene | 3587eed | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1181 | if (LHSi) { |
| 1182 | if (LHSi->getValue()) { |
| 1183 | return MHS; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1184 | } else { |
David Greene | 3587eed | 2009-05-14 23:26:46 +0000 | [diff] [blame] | 1185 | return RHS; |
| 1186 | } |
| 1187 | } |
| 1188 | break; |
| 1189 | } |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1190 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1191 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1192 | return const_cast<TernOpInit *>(this); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1193 | } |
David Greene | 5d0c051 | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 1194 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1195 | Init *TernOpInit::resolveReferences(Record &R, |
| 1196 | const RecordVal *RV) const { |
| 1197 | Init *lhs = LHS->resolveReferences(R, RV); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (Opc == IF && lhs != LHS) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1200 | IntInit *Value = dynamic_cast<IntInit*>(lhs); |
| 1201 | if (Init *I = lhs->convertInitializerTo(IntRecTy::get())) |
| 1202 | Value = dynamic_cast<IntInit*>(I); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 1203 | if (Value != 0) { |
| 1204 | // Short-circuit |
| 1205 | if (Value->getValue()) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1206 | Init *mhs = MHS->resolveReferences(R, RV); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1207 | return (TernOpInit::get(getOpcode(), lhs, mhs, |
| 1208 | RHS, getType()))->Fold(&R, 0); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1209 | } else { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1210 | Init *rhs = RHS->resolveReferences(R, RV); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1211 | return (TernOpInit::get(getOpcode(), lhs, MHS, |
| 1212 | rhs, getType()))->Fold(&R, 0); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1216 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1217 | Init *mhs = MHS->resolveReferences(R, RV); |
| 1218 | Init *rhs = RHS->resolveReferences(R, RV); |
David Greene | b035445 | 2009-06-08 19:16:56 +0000 | [diff] [blame] | 1219 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1220 | if (LHS != lhs || MHS != mhs || RHS != rhs) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1221 | return (TernOpInit::get(getOpcode(), lhs, mhs, rhs, |
| 1222 | getType()))->Fold(&R, 0); |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1223 | return Fold(&R, 0); |
| 1224 | } |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 1225 | |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1226 | std::string TernOpInit::getAsString() const { |
| 1227 | std::string Result; |
| 1228 | switch (Opc) { |
| 1229 | case SUBST: Result = "!subst"; break; |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1230 | case FOREACH: Result = "!foreach"; break; |
| 1231 | case IF: Result = "!if"; break; |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1232 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1233 | return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", " |
David Greene | 98ed3c7 | 2009-05-14 21:54:42 +0000 | [diff] [blame] | 1234 | + RHS->getAsString() + ")"; |
| 1235 | } |
David Greene | 196ac3c | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 1236 | |
David Greene | 2a9de4d | 2010-09-03 21:00:49 +0000 | [diff] [blame] | 1237 | RecTy *TypedInit::getFieldType(const std::string &FieldName) const { |
| 1238 | RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType()); |
| 1239 | if (RecordType) { |
| 1240 | RecordVal *Field = RecordType->getRecord()->getValue(FieldName); |
| 1241 | if (Field) { |
| 1242 | return Field->getType(); |
| 1243 | } |
| 1244 | } |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1248 | Init * |
David Greene | 1aa0e3e | 2011-07-29 19:07:05 +0000 | [diff] [blame] | 1249 | TypedInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1250 | BitsRecTy *T = dynamic_cast<BitsRecTy*>(getType()); |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 1251 | if (T == 0) return 0; // Cannot subscript a non-bits variable. |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1252 | unsigned NumBits = T->getNumBits(); |
| 1253 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1254 | SmallVector<Init *, 16> NewBits(Bits.size()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1255 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 1256 | if (Bits[i] >= NumBits) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1257 | return 0; |
David Greene | b3da812 | 2011-07-29 19:07:00 +0000 | [diff] [blame] | 1258 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1259 | NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), Bits[i]); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1260 | } |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1261 | return BitsInit::get(NewBits); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1264 | Init * |
David Greene | 1aa0e3e | 2011-07-29 19:07:05 +0000 | [diff] [blame] | 1265 | TypedInit::convertInitListSlice(const std::vector<unsigned> &Elements) const { |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1266 | ListRecTy *T = dynamic_cast<ListRecTy*>(getType()); |
Bill Wendling | 73a48d8 | 2010-12-10 22:54:30 +0000 | [diff] [blame] | 1267 | if (T == 0) return 0; // Cannot subscript a non-list variable. |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1268 | |
| 1269 | if (Elements.size() == 1) |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1270 | return VarListElementInit::get(const_cast<TypedInit *>(this), Elements[0]); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1271 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1272 | std::vector<Init*> ListInits; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1273 | ListInits.reserve(Elements.size()); |
| 1274 | for (unsigned i = 0, e = Elements.size(); i != e; ++i) |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1275 | ListInits.push_back(VarListElementInit::get(const_cast<TypedInit *>(this), |
| 1276 | Elements[i])); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1277 | return ListInit::get(ListInits, T); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1281 | VarInit *VarInit::get(const std::string &VN, RecTy *T) { |
David Greene | 914adf0 | 2011-10-19 13:02:33 +0000 | [diff] [blame] | 1282 | Init *Value = StringInit::get(VN); |
| 1283 | return VarInit::get(Value, T); |
| 1284 | } |
| 1285 | |
| 1286 | VarInit *VarInit::get(Init *VN, RecTy *T) { |
| 1287 | typedef std::pair<RecTy *, Init *> Key; |
David Greene | cde30d0 | 2011-07-29 19:07:21 +0000 | [diff] [blame] | 1288 | typedef DenseMap<Key, VarInit *> Pool; |
| 1289 | static Pool ThePool; |
| 1290 | |
| 1291 | Key TheKey(std::make_pair(T, VN)); |
| 1292 | |
| 1293 | VarInit *&I = ThePool[TheKey]; |
| 1294 | if (!I) I = new VarInit(VN, T); |
| 1295 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
David Greene | 914adf0 | 2011-10-19 13:02:33 +0000 | [diff] [blame] | 1298 | const std::string &VarInit::getName() const { |
| 1299 | StringInit *NameString = |
| 1300 | dynamic_cast<StringInit *>(getNameInit()); |
| 1301 | assert(NameString && "VarInit name is not a string!"); |
| 1302 | return NameString->getValue(); |
| 1303 | } |
| 1304 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1305 | Init *VarInit::getBit(unsigned Bit) const { |
| 1306 | if (getType() == BitRecTy::get()) |
| 1307 | return const_cast<VarInit*>(this); |
| 1308 | return VarBitInit::get(const_cast<VarInit*>(this), Bit); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1311 | Init *VarInit::resolveListElementReference(Record &R, |
| 1312 | const RecordVal *IRV, |
| 1313 | unsigned Elt) const { |
Jakob Stoklund Olesen | 9d1c5ee | 2012-01-13 03:16:35 +0000 | [diff] [blame] | 1314 | if (R.isTemplateArg(getNameInit())) return 0; |
| 1315 | if (IRV && IRV->getNameInit() != getNameInit()) return 0; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1316 | |
Jakob Stoklund Olesen | 9d1c5ee | 2012-01-13 03:16:35 +0000 | [diff] [blame] | 1317 | RecordVal *RV = R.getValue(getNameInit()); |
Bob Wilson | 159905a | 2009-11-21 22:44:20 +0000 | [diff] [blame] | 1318 | assert(RV && "Reference to a non-existent variable?"); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1319 | ListInit *LI = dynamic_cast<ListInit*>(RV->getValue()); |
David Greene | 9d3febe | 2009-05-14 20:38:52 +0000 | [diff] [blame] | 1320 | if (!LI) { |
David Greene | a8b8ab6 | 2011-10-04 18:55:36 +0000 | [diff] [blame] | 1321 | TypedInit *VI = dynamic_cast<TypedInit*>(RV->getValue()); |
David Greene | 9d3febe | 2009-05-14 20:38:52 +0000 | [diff] [blame] | 1322 | assert(VI && "Invalid list element!"); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1323 | return VarListElementInit::get(VI, Elt); |
David Greene | 9d3febe | 2009-05-14 20:38:52 +0000 | [diff] [blame] | 1324 | } |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1325 | |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1326 | if (Elt >= LI->getSize()) |
| 1327 | return 0; // Out of range reference. |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1328 | Init *E = LI->getElement(Elt); |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1329 | // If the element is set to some value, or if we are resolving a reference |
| 1330 | // to a specific variable and that variable is explicitly unset, then |
| 1331 | // replace the VarListElementInit with it. |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1332 | if (IRV || !dynamic_cast<UnsetInit*>(E)) |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1333 | return E; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1338 | RecTy *VarInit::getFieldType(const std::string &FieldName) const { |
| 1339 | if (RecordRecTy *RTy = dynamic_cast<RecordRecTy*>(getType())) |
| 1340 | if (const RecordVal *RV = RTy->getRecord()->getValue(FieldName)) |
| 1341 | return RV->getType(); |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1345 | Init *VarInit::getFieldInit(Record &R, const RecordVal *RV, |
| 1346 | const std::string &FieldName) const { |
Reid Spencer | 2a82686 | 2006-11-02 20:46:16 +0000 | [diff] [blame] | 1347 | if (dynamic_cast<RecordRecTy*>(getType())) |
Jakob Stoklund Olesen | 0e45762 | 2010-03-25 06:23:34 +0000 | [diff] [blame] | 1348 | if (const RecordVal *Val = R.getValue(VarName)) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1349 | if (RV != Val && (RV || dynamic_cast<UnsetInit*>(Val->getValue()))) |
Jakob Stoklund Olesen | 0e45762 | 2010-03-25 06:23:34 +0000 | [diff] [blame] | 1350 | return 0; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1351 | Init *TheInit = Val->getValue(); |
Chris Lattner | d959ab9 | 2004-02-28 16:31:53 +0000 | [diff] [blame] | 1352 | assert(TheInit != this && "Infinite loop detected!"); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1353 | if (Init *I = TheInit->getFieldInit(R, RV, FieldName)) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1354 | return I; |
| 1355 | else |
| 1356 | return 0; |
Chris Lattner | d959ab9 | 2004-02-28 16:31:53 +0000 | [diff] [blame] | 1357 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | /// resolveReferences - This method is used by classes that refer to other |
Bob Wilson | 159905a | 2009-11-21 22:44:20 +0000 | [diff] [blame] | 1362 | /// 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] | 1363 | /// If a value is set for the variable later, this method will be called on |
| 1364 | /// users of the value to allow the value to propagate out. |
| 1365 | /// |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1366 | Init *VarInit::resolveReferences(Record &R, const RecordVal *RV) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1367 | if (RecordVal *Val = R.getValue(VarName)) |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1368 | if (RV == Val || (RV == 0 && !dynamic_cast<UnsetInit*>(Val->getValue()))) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1369 | return Val->getValue(); |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1370 | return const_cast<VarInit *>(this); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1371 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 1372 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1373 | VarBitInit *VarBitInit::get(TypedInit *T, unsigned B) { |
| 1374 | typedef std::pair<TypedInit *, unsigned> Key; |
David Greene | 9aa8284 | 2011-07-29 19:07:22 +0000 | [diff] [blame] | 1375 | typedef DenseMap<Key, VarBitInit *> Pool; |
| 1376 | |
| 1377 | static Pool ThePool; |
| 1378 | |
| 1379 | Key TheKey(std::make_pair(T, B)); |
| 1380 | |
| 1381 | VarBitInit *&I = ThePool[TheKey]; |
| 1382 | if (!I) I = new VarBitInit(T, B); |
| 1383 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1386 | std::string VarBitInit::getAsString() const { |
| 1387 | return TI->getAsString() + "{" + utostr(Bit) + "}"; |
| 1388 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1389 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1390 | Init *VarBitInit::resolveReferences(Record &R, const RecordVal *RV) const { |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1391 | Init *I = TI->resolveReferences(R, RV); |
| 1392 | if (TI != I) |
| 1393 | return I->getBit(getBitNum()); |
| 1394 | |
| 1395 | return const_cast<VarBitInit*>(this); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1398 | VarListElementInit *VarListElementInit::get(TypedInit *T, |
| 1399 | unsigned E) { |
| 1400 | typedef std::pair<TypedInit *, unsigned> Key; |
David Greene | 7f501e8 | 2011-07-29 19:07:23 +0000 | [diff] [blame] | 1401 | typedef DenseMap<Key, VarListElementInit *> Pool; |
| 1402 | |
| 1403 | static Pool ThePool; |
| 1404 | |
| 1405 | Key TheKey(std::make_pair(T, E)); |
| 1406 | |
| 1407 | VarListElementInit *&I = ThePool[TheKey]; |
| 1408 | if (!I) I = new VarListElementInit(T, E); |
| 1409 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1412 | std::string VarListElementInit::getAsString() const { |
| 1413 | return TI->getAsString() + "[" + utostr(Element) + "]"; |
| 1414 | } |
| 1415 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1416 | Init * |
| 1417 | VarListElementInit::resolveReferences(Record &R, const RecordVal *RV) const { |
| 1418 | if (Init *I = getVariable()->resolveListElementReference(R, RV, |
Eric Christopher | 71520a8 | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 1419 | getElementNum())) |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1420 | return I; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1421 | return const_cast<VarListElementInit *>(this); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1424 | Init *VarListElementInit::getBit(unsigned Bit) const { |
| 1425 | if (getType() == BitRecTy::get()) |
| 1426 | return const_cast<VarListElementInit*>(this); |
| 1427 | return VarBitInit::get(const_cast<VarListElementInit*>(this), Bit); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1430 | Init *VarListElementInit:: resolveListElementReference(Record &R, |
| 1431 | const RecordVal *RV, |
| 1432 | unsigned Elt) const { |
David Greene | 74ce80f | 2011-09-30 20:59:49 +0000 | [diff] [blame] | 1433 | Init *Result = TI->resolveListElementReference(R, RV, Element); |
| 1434 | |
| 1435 | if (Result) { |
| 1436 | TypedInit *TInit = dynamic_cast<TypedInit *>(Result); |
| 1437 | if (TInit) { |
David Greene | 7475ad0 | 2011-10-06 21:20:46 +0000 | [diff] [blame] | 1438 | Init *Result2 = TInit->resolveListElementReference(R, RV, Elt); |
| 1439 | if (Result2) return Result2; |
| 1440 | return new VarListElementInit(TInit, Elt); |
David Greene | 74ce80f | 2011-09-30 20:59:49 +0000 | [diff] [blame] | 1441 | } |
| 1442 | return Result; |
| 1443 | } |
| 1444 | |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1445 | return 0; |
| 1446 | } |
| 1447 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1448 | DefInit *DefInit::get(Record *R) { |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 1449 | return R->getDefInit(); |
| 1450 | } |
| 1451 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1452 | RecTy *DefInit::getFieldType(const std::string &FieldName) const { |
| 1453 | if (const RecordVal *RV = Def->getValue(FieldName)) |
| 1454 | return RV->getType(); |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1458 | Init *DefInit::getFieldInit(Record &R, const RecordVal *RV, |
| 1459 | const std::string &FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1460 | return Def->getValue(FieldName)->getValue(); |
| 1461 | } |
| 1462 | |
| 1463 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1464 | std::string DefInit::getAsString() const { |
| 1465 | return Def->getName(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1468 | FieldInit *FieldInit::get(Init *R, const std::string &FN) { |
| 1469 | typedef std::pair<Init *, TableGenStringKey> Key; |
David Greene | 760f867 | 2011-07-29 19:07:24 +0000 | [diff] [blame] | 1470 | typedef DenseMap<Key, FieldInit *> Pool; |
| 1471 | static Pool ThePool; |
| 1472 | |
| 1473 | Key TheKey(std::make_pair(R, FN)); |
| 1474 | |
| 1475 | FieldInit *&I = ThePool[TheKey]; |
| 1476 | if (!I) I = new FieldInit(R, FN); |
| 1477 | return I; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1480 | Init *FieldInit::getBit(unsigned Bit) const { |
| 1481 | if (getType() == BitRecTy::get()) |
| 1482 | return const_cast<FieldInit*>(this); |
| 1483 | return VarBitInit::get(const_cast<FieldInit*>(this), Bit); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1486 | Init *FieldInit::resolveListElementReference(Record &R, const RecordVal *RV, |
| 1487 | unsigned Elt) const { |
| 1488 | if (Init *ListVal = Rec->getFieldInit(R, RV, FieldName)) |
| 1489 | if (ListInit *LI = dynamic_cast<ListInit*>(ListVal)) { |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1490 | if (Elt >= LI->getSize()) return 0; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1491 | Init *E = LI->getElement(Elt); |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1492 | |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1493 | // If the element is set to some value, or if we are resolving a |
| 1494 | // reference to a specific variable and that variable is explicitly |
| 1495 | // unset, then replace the VarListElementInit with it. |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1496 | if (RV || !dynamic_cast<UnsetInit*>(E)) |
Bob Wilson | 67e6cab | 2009-11-22 03:58:57 +0000 | [diff] [blame] | 1497 | return E; |
Chris Lattner | 577fc3f | 2004-07-27 01:01:21 +0000 | [diff] [blame] | 1498 | } |
| 1499 | return 0; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1502 | Init *FieldInit::resolveReferences(Record &R, const RecordVal *RV) const { |
| 1503 | Init *NewRec = RV ? Rec->resolveReferences(R, RV) : Rec; |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1504 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1505 | Init *BitsVal = NewRec->getFieldInit(R, RV, FieldName); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1506 | if (BitsVal) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1507 | Init *BVR = BitsVal->resolveReferences(R, RV); |
| 1508 | return BVR->isComplete() ? BVR : const_cast<FieldInit *>(this); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1509 | } |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1510 | |
| 1511 | if (NewRec != Rec) { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1512 | return FieldInit::get(NewRec, FieldName); |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1513 | } |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1514 | return const_cast<FieldInit *>(this); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
David Greene | a74bd90 | 2011-07-29 19:07:26 +0000 | [diff] [blame] | 1517 | void ProfileDagInit(FoldingSetNodeID &ID, |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1518 | Init *V, |
David Greene | a74bd90 | 2011-07-29 19:07:26 +0000 | [diff] [blame] | 1519 | const std::string &VN, |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1520 | ArrayRef<Init *> ArgRange, |
David Greene | a74bd90 | 2011-07-29 19:07:26 +0000 | [diff] [blame] | 1521 | ArrayRef<std::string> NameRange) { |
| 1522 | ID.AddPointer(V); |
| 1523 | ID.AddString(VN); |
| 1524 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1525 | ArrayRef<Init *>::iterator Arg = ArgRange.begin(); |
David Greene | a74bd90 | 2011-07-29 19:07:26 +0000 | [diff] [blame] | 1526 | ArrayRef<std::string>::iterator Name = NameRange.begin(); |
| 1527 | while (Arg != ArgRange.end()) { |
| 1528 | assert(Name != NameRange.end() && "Arg name underflow!"); |
| 1529 | ID.AddPointer(*Arg++); |
| 1530 | ID.AddString(*Name++); |
| 1531 | } |
| 1532 | assert(Name == NameRange.end() && "Arg name overflow!"); |
| 1533 | } |
| 1534 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1535 | DagInit * |
| 1536 | DagInit::get(Init *V, const std::string &VN, |
| 1537 | ArrayRef<Init *> ArgRange, |
David Greene | a74bd90 | 2011-07-29 19:07:26 +0000 | [diff] [blame] | 1538 | ArrayRef<std::string> NameRange) { |
| 1539 | typedef FoldingSet<DagInit> Pool; |
| 1540 | static Pool ThePool; |
| 1541 | |
| 1542 | FoldingSetNodeID ID; |
| 1543 | ProfileDagInit(ID, V, VN, ArgRange, NameRange); |
| 1544 | |
| 1545 | void *IP = 0; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1546 | if (DagInit *I = ThePool.FindNodeOrInsertPos(ID, IP)) |
David Greene | a74bd90 | 2011-07-29 19:07:26 +0000 | [diff] [blame] | 1547 | return I; |
| 1548 | |
| 1549 | DagInit *I = new DagInit(V, VN, ArgRange, NameRange); |
| 1550 | ThePool.InsertNode(I, IP); |
| 1551 | |
| 1552 | return I; |
| 1553 | } |
| 1554 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1555 | DagInit * |
| 1556 | DagInit::get(Init *V, const std::string &VN, |
| 1557 | const std::vector<std::pair<Init*, std::string> > &args) { |
| 1558 | typedef std::pair<Init*, std::string> PairType; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1559 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1560 | std::vector<Init *> Args; |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1561 | std::vector<std::string> Names; |
| 1562 | |
| 1563 | for (std::vector<PairType>::const_iterator i = args.begin(), |
| 1564 | iend = args.end(); |
| 1565 | i != iend; |
| 1566 | ++i) { |
| 1567 | Args.push_back(i->first); |
| 1568 | Names.push_back(i->second); |
| 1569 | } |
| 1570 | |
| 1571 | return DagInit::get(V, VN, Args, Names); |
| 1572 | } |
| 1573 | |
David Greene | a74bd90 | 2011-07-29 19:07:26 +0000 | [diff] [blame] | 1574 | void DagInit::Profile(FoldingSetNodeID &ID) const { |
| 1575 | ProfileDagInit(ID, Val, ValName, Args, ArgNames); |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1578 | Init *DagInit::resolveReferences(Record &R, const RecordVal *RV) const { |
| 1579 | std::vector<Init*> NewArgs; |
Chris Lattner | 0d3ef40 | 2006-01-31 06:02:35 +0000 | [diff] [blame] | 1580 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 1581 | NewArgs.push_back(Args[i]->resolveReferences(R, RV)); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1582 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1583 | Init *Op = Val->resolveReferences(R, RV); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | b59cf3c | 2006-03-30 22:50:40 +0000 | [diff] [blame] | 1585 | if (Args != NewArgs || Op != Val) |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1586 | return DagInit::get(Op, ValName, NewArgs, ArgNames); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1587 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1588 | return const_cast<DagInit *>(this); |
Chris Lattner | 0d3ef40 | 2006-01-31 06:02:35 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1591 | |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1592 | std::string DagInit::getAsString() const { |
| 1593 | std::string Result = "(" + Val->getAsString(); |
Nate Begeman | dbe3f77 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1594 | if (!ValName.empty()) |
| 1595 | Result += ":" + ValName; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1596 | if (Args.size()) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1597 | Result += " " + Args[0]->getAsString(); |
| 1598 | if (!ArgNames[0].empty()) Result += ":$" + ArgNames[0]; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1599 | for (unsigned i = 1, e = Args.size(); i != e; ++i) { |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1600 | Result += ", " + Args[i]->getAsString(); |
| 1601 | if (!ArgNames[i].empty()) Result += ":$" + ArgNames[i]; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } |
Chris Lattner | 695506c | 2007-11-22 21:05:25 +0000 | [diff] [blame] | 1604 | return Result + ")"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | |
| 1608 | //===----------------------------------------------------------------------===// |
| 1609 | // Other implementations |
| 1610 | //===----------------------------------------------------------------------===// |
| 1611 | |
David Greene | 09d153e | 2011-09-02 20:12:07 +0000 | [diff] [blame] | 1612 | RecordVal::RecordVal(Init *N, RecTy *T, unsigned P) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1613 | : Name(N), Ty(T), Prefix(P) { |
David Greene | e32ebf2 | 2011-07-29 19:07:07 +0000 | [diff] [blame] | 1614 | Value = Ty->convertValue(UnsetInit::get()); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1615 | assert(Value && "Cannot create unset value for current type!"); |
| 1616 | } |
| 1617 | |
David Greene | 09d153e | 2011-09-02 20:12:07 +0000 | [diff] [blame] | 1618 | RecordVal::RecordVal(const std::string &N, RecTy *T, unsigned P) |
| 1619 | : Name(StringInit::get(N)), Ty(T), Prefix(P) { |
| 1620 | Value = Ty->convertValue(UnsetInit::get()); |
| 1621 | assert(Value && "Cannot create unset value for current type!"); |
| 1622 | } |
| 1623 | |
| 1624 | const std::string &RecordVal::getName() const { |
| 1625 | StringInit *NameString = dynamic_cast<StringInit *>(Name); |
| 1626 | assert(NameString && "RecordVal name is not a string!"); |
| 1627 | return NameString->getValue(); |
| 1628 | } |
| 1629 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1630 | void RecordVal::dump() const { errs() << *this; } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1631 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1632 | void RecordVal::print(raw_ostream &OS, bool PrintSem) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1633 | if (getPrefix()) OS << "field "; |
David Greene | 658a4b7 | 2011-10-19 13:02:52 +0000 | [diff] [blame] | 1634 | OS << *getType() << " " << getNameInitAsString(); |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1635 | |
| 1636 | if (getValue()) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1637 | OS << " = " << *getValue(); |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1639 | if (PrintSem) OS << ";\n"; |
| 1640 | } |
| 1641 | |
Daniel Dunbar | ced0081 | 2009-08-23 09:47:37 +0000 | [diff] [blame] | 1642 | unsigned Record::LastID = 0; |
| 1643 | |
David Greene | b77fc0d | 2011-10-19 13:02:45 +0000 | [diff] [blame] | 1644 | void Record::init() { |
| 1645 | checkName(); |
David Greene | d699161 | 2011-10-19 13:04:13 +0000 | [diff] [blame] | 1646 | |
| 1647 | // Every record potentially has a def at the top. This value is |
| 1648 | // replaced with the top-level def name at instantiation time. |
| 1649 | RecordVal DN("NAME", StringRecTy::get(), 0); |
| 1650 | addValue(DN); |
David Greene | b77fc0d | 2011-10-19 13:02:45 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
David Greene | 50c0912 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 1653 | void Record::checkName() { |
| 1654 | // Ensure the record name has string type. |
| 1655 | const TypedInit *TypedName = dynamic_cast<const TypedInit *>(Name); |
| 1656 | assert(TypedName && "Record name is not typed!"); |
| 1657 | RecTy *Type = TypedName->getType(); |
| 1658 | if (dynamic_cast<StringRecTy *>(Type) == 0) { |
Jim Grosbach | d2aabd3 | 2012-07-12 00:53:31 +0000 | [diff] [blame] | 1659 | throw TGError(getLoc(), "Record name is not a string!"); |
David Greene | 50c0912 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
| 1662 | |
Jakob Stoklund Olesen | abcfdce | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 1663 | DefInit *Record::getDefInit() { |
| 1664 | if (!TheInit) |
| 1665 | TheInit = new DefInit(this, new RecordRecTy(this)); |
| 1666 | return TheInit; |
| 1667 | } |
| 1668 | |
David Greene | 50c0912 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 1669 | const std::string &Record::getName() const { |
| 1670 | const StringInit *NameString = |
| 1671 | dynamic_cast<const StringInit *>(Name); |
| 1672 | assert(NameString && "Record name is not a string!"); |
| 1673 | return NameString->getValue(); |
| 1674 | } |
| 1675 | |
| 1676 | void Record::setName(Init *NewName) { |
| 1677 | if (TrackedRecords.getDef(Name->getAsUnquotedString()) == this) { |
| 1678 | TrackedRecords.removeDef(Name->getAsUnquotedString()); |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1679 | TrackedRecords.addDef(this); |
David Greene | f651a2a | 2011-10-19 13:03:25 +0000 | [diff] [blame] | 1680 | } else if (TrackedRecords.getClass(Name->getAsUnquotedString()) == this) { |
David Greene | 50c0912 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 1681 | TrackedRecords.removeClass(Name->getAsUnquotedString()); |
Chris Lattner | 77d369c | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 1682 | TrackedRecords.addClass(this); |
David Greene | f651a2a | 2011-10-19 13:03:25 +0000 | [diff] [blame] | 1683 | } // Otherwise this isn't yet registered. |
| 1684 | Name = NewName; |
David Greene | 50c0912 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 1685 | checkName(); |
David Greene | 50c0912 | 2011-08-10 18:27:46 +0000 | [diff] [blame] | 1686 | // DO NOT resolve record values to the name at this point because |
| 1687 | // there might be default values for arguments of this def. Those |
| 1688 | // arguments might not have been resolved yet so we don't want to |
| 1689 | // prematurely assume values for those arguments were not passed to |
| 1690 | // this def. |
| 1691 | // |
| 1692 | // Nonetheless, it may be that some of this Record's values |
| 1693 | // reference the record name. Indeed, the reason for having the |
| 1694 | // record name be an Init is to provide this flexibility. The extra |
| 1695 | // resolve steps after completely instantiating defs takes care of |
| 1696 | // this. See TGParser::ParseDef and TGParser::ParseDefm. |
| 1697 | } |
| 1698 | |
| 1699 | void Record::setName(const std::string &Name) { |
| 1700 | setName(StringInit::get(Name)); |
Chris Lattner | ac28425 | 2005-08-19 17:58:11 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1703 | /// resolveReferencesTo - If anything in this record refers to RV, replace the |
| 1704 | /// reference to RV with the RHS of RV. If RV is null, we resolve all possible |
| 1705 | /// references. |
| 1706 | void Record::resolveReferencesTo(const RecordVal *RV) { |
| 1707 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
Jakob Stoklund Olesen | aa0f752 | 2012-03-07 16:39:35 +0000 | [diff] [blame] | 1708 | if (RV == &Values[i]) // Skip resolve the same field as the given one |
| 1709 | continue; |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1710 | if (Init *V = Values[i].getValue()) |
Michael Liao | 026f833 | 2012-09-06 23:32:48 +0000 | [diff] [blame] | 1711 | if (Values[i].setValue(V->resolveReferences(*this, RV))) |
| 1712 | throw TGError(getLoc(), "Invalid value is found when setting '" |
| 1713 | + Values[i].getNameInitAsString() |
| 1714 | + "' after resolving references" |
| 1715 | + (RV ? " against '" + RV->getNameInitAsString() |
| 1716 | + "' of (" |
| 1717 | + RV->getValue()->getAsUnquotedString() + ")" |
| 1718 | : "") |
| 1719 | + "\n"); |
Chris Lattner | ef94374 | 2005-04-19 03:36:21 +0000 | [diff] [blame] | 1720 | } |
David Greene | 9effff2 | 2011-10-19 13:03:30 +0000 | [diff] [blame] | 1721 | Init *OldName = getNameInit(); |
| 1722 | Init *NewName = Name->resolveReferences(*this, RV); |
| 1723 | if (NewName != OldName) { |
| 1724 | // Re-register with RecordKeeper. |
| 1725 | setName(NewName); |
| 1726 | } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1729 | void Record::dump() const { errs() << *this; } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1730 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1731 | raw_ostream &llvm::operator<<(raw_ostream &OS, const Record &R) { |
David Greene | 98be78a | 2011-10-19 13:02:57 +0000 | [diff] [blame] | 1732 | OS << R.getNameInitAsString(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1733 | |
David Greene | db10e69 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 1734 | const std::vector<Init *> &TArgs = R.getTemplateArgs(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1735 | if (!TArgs.empty()) { |
| 1736 | OS << "<"; |
| 1737 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { |
| 1738 | if (i) OS << ", "; |
| 1739 | const RecordVal *RV = R.getValue(TArgs[i]); |
| 1740 | assert(RV && "Template argument record not found??"); |
| 1741 | RV->print(OS, false); |
| 1742 | } |
| 1743 | OS << ">"; |
| 1744 | } |
| 1745 | |
| 1746 | OS << " {"; |
| 1747 | const std::vector<Record*> &SC = R.getSuperClasses(); |
| 1748 | if (!SC.empty()) { |
| 1749 | OS << "\t//"; |
| 1750 | for (unsigned i = 0, e = SC.size(); i != e; ++i) |
David Greene | 077d84e | 2011-10-19 13:03:02 +0000 | [diff] [blame] | 1751 | OS << " " << SC[i]->getNameInitAsString(); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1752 | } |
| 1753 | OS << "\n"; |
| 1754 | |
| 1755 | const std::vector<RecordVal> &Vals = R.getValues(); |
| 1756 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 1757 | if (Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) |
| 1758 | OS << Vals[i]; |
| 1759 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) |
| 1760 | if (!Vals[i].getPrefix() && !R.isTemplateArg(Vals[i].getName())) |
| 1761 | OS << Vals[i]; |
| 1762 | |
| 1763 | return OS << "}\n"; |
| 1764 | } |
| 1765 | |
| 1766 | /// getValueInit - Return the initializer for a value with the specified name, |
| 1767 | /// or throw an exception if the field does not exist. |
| 1768 | /// |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1769 | Init *Record::getValueInit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1770 | const RecordVal *R = getValue(FieldName); |
| 1771 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1772 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1773 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1774 | return R->getValue(); |
| 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | /// getValueAsString - This method looks up the specified field and returns its |
| 1779 | /// value as a string, throwing an exception if the field does not exist or if |
| 1780 | /// the value is not a string. |
| 1781 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1782 | std::string Record::getValueAsString(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1783 | const RecordVal *R = getValue(FieldName); |
| 1784 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1785 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1786 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1787 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1788 | if (StringInit *SI = dynamic_cast<StringInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1789 | return SI->getValue(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1790 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1791 | "' does not have a string initializer!"; |
| 1792 | } |
| 1793 | |
| 1794 | /// getValueAsBitsInit - This method looks up the specified field and returns |
| 1795 | /// its value as a BitsInit, throwing an exception if the field does not exist |
| 1796 | /// or if the value is not the right type. |
| 1797 | /// |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1798 | BitsInit *Record::getValueAsBitsInit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1799 | const RecordVal *R = getValue(FieldName); |
| 1800 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1801 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1802 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1803 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1804 | if (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1805 | return BI; |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1806 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1807 | "' does not have a BitsInit initializer!"; |
| 1808 | } |
| 1809 | |
| 1810 | /// getValueAsListInit - This method looks up the specified field and returns |
| 1811 | /// its value as a ListInit, throwing an exception if the field does not exist |
| 1812 | /// or if the value is not the right type. |
| 1813 | /// |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1814 | ListInit *Record::getValueAsListInit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1815 | const RecordVal *R = getValue(FieldName); |
| 1816 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1817 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1818 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1819 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1820 | if (ListInit *LI = dynamic_cast<ListInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1821 | return LI; |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1822 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1823 | "' does not have a list initializer!"; |
| 1824 | } |
| 1825 | |
Chris Lattner | 7ad0bed | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 1826 | /// getValueAsListOfDefs - This method looks up the specified field and returns |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1827 | /// its value as a vector of records, throwing an exception if the field does |
| 1828 | /// not exist or if the value is not the right type. |
| 1829 | /// |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1830 | std::vector<Record*> |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1831 | Record::getValueAsListOfDefs(StringRef FieldName) const { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1832 | ListInit *List = getValueAsListInit(FieldName); |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1833 | std::vector<Record*> Defs; |
| 1834 | for (unsigned i = 0; i < List->getSize(); i++) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1835 | if (DefInit *DI = dynamic_cast<DefInit*>(List->getElement(i))) { |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1836 | Defs.push_back(DI->getDef()); |
| 1837 | } else { |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1838 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Jim Laskey | b04feb6 | 2005-10-28 21:46:31 +0000 | [diff] [blame] | 1839 | "' list is not entirely DefInit!"; |
| 1840 | } |
| 1841 | } |
| 1842 | return Defs; |
| 1843 | } |
| 1844 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1845 | /// getValueAsInt - This method looks up the specified field and returns its |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 1846 | /// 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] | 1847 | /// the value is not the right type. |
| 1848 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1849 | int64_t Record::getValueAsInt(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1850 | const RecordVal *R = getValue(FieldName); |
| 1851 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1852 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1853 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1854 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1855 | if (IntInit *II = dynamic_cast<IntInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1856 | return II->getValue(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1857 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Nate Begeman | f621b33 | 2005-11-30 18:37:14 +0000 | [diff] [blame] | 1858 | "' does not have an int initializer!"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1861 | /// getValueAsListOfInts - This method looks up the specified field and returns |
| 1862 | /// its value as a vector of integers, throwing an exception if the field does |
| 1863 | /// not exist or if the value is not the right type. |
| 1864 | /// |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1865 | std::vector<int64_t> |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1866 | Record::getValueAsListOfInts(StringRef FieldName) const { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1867 | ListInit *List = getValueAsListInit(FieldName); |
Dan Gohman | ca0546f | 2008-10-17 01:33:43 +0000 | [diff] [blame] | 1868 | std::vector<int64_t> Ints; |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1869 | for (unsigned i = 0; i < List->getSize(); i++) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1870 | if (IntInit *II = dynamic_cast<IntInit*>(List->getElement(i))) { |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1871 | Ints.push_back(II->getValue()); |
| 1872 | } else { |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1873 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Anton Korobeynikov | a468a11 | 2007-11-11 11:19:37 +0000 | [diff] [blame] | 1874 | "' does not have a list of ints initializer!"; |
| 1875 | } |
| 1876 | } |
| 1877 | return Ints; |
| 1878 | } |
| 1879 | |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1880 | /// getValueAsListOfStrings - This method looks up the specified field and |
| 1881 | /// returns its value as a vector of strings, throwing an exception if the |
| 1882 | /// field does not exist or if the value is not the right type. |
| 1883 | /// |
| 1884 | std::vector<std::string> |
| 1885 | Record::getValueAsListOfStrings(StringRef FieldName) const { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1886 | ListInit *List = getValueAsListInit(FieldName); |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1887 | std::vector<std::string> Strings; |
| 1888 | for (unsigned i = 0; i < List->getSize(); i++) { |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1889 | if (StringInit *II = dynamic_cast<StringInit*>(List->getElement(i))) { |
Owen Anderson | a84be6c | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 1890 | Strings.push_back(II->getValue()); |
| 1891 | } else { |
| 1892 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
| 1893 | "' does not have a list of strings initializer!"; |
| 1894 | } |
| 1895 | } |
| 1896 | return Strings; |
| 1897 | } |
| 1898 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1899 | /// getValueAsDef - This method looks up the specified field and returns its |
| 1900 | /// value as a Record, throwing an exception if the field does not exist or if |
| 1901 | /// the value is not the right type. |
| 1902 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1903 | Record *Record::getValueAsDef(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1904 | const RecordVal *R = getValue(FieldName); |
| 1905 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1906 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1907 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1908 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1909 | if (DefInit *DI = dynamic_cast<DefInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1910 | return DI->getDef(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1911 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Nate Begeman | f621b33 | 2005-11-30 18:37:14 +0000 | [diff] [blame] | 1912 | "' does not have a def initializer!"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /// getValueAsBit - This method looks up the specified field and returns its |
| 1916 | /// value as a bit, throwing an exception if the field does not exist or if |
| 1917 | /// the value is not the right type. |
| 1918 | /// |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1919 | bool Record::getValueAsBit(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1920 | const RecordVal *R = getValue(FieldName); |
| 1921 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1922 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1923 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1924 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1925 | if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1926 | return BI->getValue(); |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1927 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1928 | "' does not have a bit initializer!"; |
| 1929 | } |
| 1930 | |
Jakob Stoklund Olesen | af507bf | 2012-08-23 19:34:46 +0000 | [diff] [blame] | 1931 | bool Record::getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const { |
| 1932 | const RecordVal *R = getValue(FieldName); |
| 1933 | if (R == 0 || R->getValue() == 0) |
| 1934 | throw "Record `" + getName() + "' does not have a field named `" + |
| 1935 | FieldName.str() + "'!\n"; |
| 1936 | |
| 1937 | if (R->getValue() == UnsetInit::get()) { |
| 1938 | Unset = true; |
| 1939 | return false; |
| 1940 | } |
| 1941 | Unset = false; |
| 1942 | if (BitInit *BI = dynamic_cast<BitInit*>(R->getValue())) |
| 1943 | return BI->getValue(); |
| 1944 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
| 1945 | "' does not have a bit initializer!"; |
| 1946 | } |
| 1947 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1948 | /// getValueAsDag - This method looks up the specified field and returns its |
| 1949 | /// value as an Dag, throwing an exception if the field does not exist or if |
| 1950 | /// the value is not the right type. |
| 1951 | /// |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1952 | DagInit *Record::getValueAsDag(StringRef FieldName) const { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1953 | const RecordVal *R = getValue(FieldName); |
| 1954 | if (R == 0 || R->getValue() == 0) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 1955 | throw "Record `" + getName() + "' does not have a field named `" + |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1956 | FieldName.str() + "'!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1957 | |
David Greene | af8ee2c | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 1958 | if (DagInit *DI = dynamic_cast<DagInit*>(R->getValue())) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1959 | return DI; |
Chris Lattner | 42bb0c1 | 2009-09-18 18:31:37 +0000 | [diff] [blame] | 1960 | throw "Record `" + getName() + "', field `" + FieldName.str() + |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1961 | "' does not have a dag initializer!"; |
| 1962 | } |
| 1963 | |
| 1964 | |
David Greene | 7049e79 | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1965 | void MultiClass::dump() const { |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1966 | errs() << "Record:\n"; |
David Greene | 7049e79 | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1967 | Rec.dump(); |
Bob Wilson | 7248f86 | 2009-11-22 04:24:42 +0000 | [diff] [blame] | 1968 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1969 | errs() << "Defs:\n"; |
David Greene | 7049e79 | 2009-04-24 16:55:41 +0000 | [diff] [blame] | 1970 | for (RecordVector::const_iterator r = DefPrototypes.begin(), |
| 1971 | rend = DefPrototypes.end(); |
| 1972 | r != rend; |
| 1973 | ++r) { |
| 1974 | (*r)->dump(); |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1979 | void RecordKeeper::dump() const { errs() << *this; } |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1980 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1981 | raw_ostream &llvm::operator<<(raw_ostream &OS, const RecordKeeper &RK) { |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1982 | OS << "------------- Classes -----------------\n"; |
| 1983 | const std::map<std::string, Record*> &Classes = RK.getClasses(); |
| 1984 | for (std::map<std::string, Record*>::const_iterator I = Classes.begin(), |
Jeff Cohen | 88e7b72 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 1985 | E = Classes.end(); I != E; ++I) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1986 | OS << "class " << *I->second; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 1987 | |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1988 | OS << "------------- Defs -----------------\n"; |
| 1989 | const std::map<std::string, Record*> &Defs = RK.getDefs(); |
| 1990 | for (std::map<std::string, Record*>::const_iterator I = Defs.begin(), |
Jeff Cohen | 88e7b72 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 1991 | E = Defs.end(); I != E; ++I) |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 1992 | OS << "def " << *I->second; |
| 1993 | return OS; |
| 1994 | } |
| 1995 | |
| 1996 | |
| 1997 | /// getAllDerivedDefinitions - This method returns all concrete definitions |
| 1998 | /// that derive from the specified class name. If a class with the specified |
| 1999 | /// name does not exist, an error is printed and true is returned. |
| 2000 | std::vector<Record*> |
| 2001 | RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const { |
Chris Lattner | 9704907 | 2010-12-13 00:20:52 +0000 | [diff] [blame] | 2002 | Record *Class = getClass(ClassName); |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 2003 | if (!Class) |
Misha Brukman | 41f9f29 | 2004-10-08 14:59:05 +0000 | [diff] [blame] | 2004 | throw "ERROR: Couldn't find the `" + ClassName + "' class!\n"; |
Chris Lattner | f5bd1b7 | 2003-10-05 19:27:59 +0000 | [diff] [blame] | 2005 | |
| 2006 | std::vector<Record*> Defs; |
| 2007 | for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(), |
| 2008 | E = getDefs().end(); I != E; ++I) |
| 2009 | if (I->second->isSubClassOf(Class)) |
| 2010 | Defs.push_back(I->second); |
| 2011 | |
| 2012 | return Defs; |
| 2013 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 2014 | |
David Greene | e714512 | 2011-10-19 13:02:36 +0000 | [diff] [blame] | 2015 | /// QualifyName - Return an Init with a qualifier prefix referring |
| 2016 | /// to CurRec's name. |
| 2017 | Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, |
| 2018 | Init *Name, const std::string &Scoper) { |
| 2019 | RecTy *Type = dynamic_cast<TypedInit *>(Name)->getType(); |
| 2020 | |
| 2021 | BinOpInit *NewName = |
| 2022 | BinOpInit::get(BinOpInit::STRCONCAT, |
| 2023 | BinOpInit::get(BinOpInit::STRCONCAT, |
| 2024 | CurRec.getNameInit(), |
| 2025 | StringInit::get(Scoper), |
| 2026 | Type)->Fold(&CurRec, CurMultiClass), |
| 2027 | Name, |
| 2028 | Type); |
| 2029 | |
| 2030 | if (CurMultiClass && Scoper != "::") { |
| 2031 | NewName = |
| 2032 | BinOpInit::get(BinOpInit::STRCONCAT, |
| 2033 | BinOpInit::get(BinOpInit::STRCONCAT, |
| 2034 | CurMultiClass->Rec.getNameInit(), |
| 2035 | StringInit::get("::"), |
| 2036 | Type)->Fold(&CurRec, CurMultiClass), |
| 2037 | NewName->Fold(&CurRec, CurMultiClass), |
| 2038 | Type); |
| 2039 | } |
| 2040 | |
| 2041 | return NewName->Fold(&CurRec, CurMultiClass); |
| 2042 | } |
| 2043 | |
| 2044 | /// QualifyName - Return an Init with a qualifier prefix referring |
| 2045 | /// to CurRec's name. |
| 2046 | Init *llvm::QualifyName(Record &CurRec, MultiClass *CurMultiClass, |
| 2047 | const std::string &Name, |
| 2048 | const std::string &Scoper) { |
| 2049 | return QualifyName(CurRec, CurMultiClass, StringInit::get(Name), Scoper); |
| 2050 | } |