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