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