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