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