Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1 | //===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the InlineAsm class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/InlineAsm.h" |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 15 | #include "ConstantsContext.h" |
| 16 | #include "LLVMContextImpl.h" |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Jeff Cohen | b24b66f | 2006-02-01 04:37:04 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 19 | #include <cctype> |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 22 | // Implement the first virtual method in this class in this file so the |
| 23 | // InlineAsm vtable is emitted here. |
| 24 | InlineAsm::~InlineAsm() { |
| 25 | } |
| 26 | |
| 27 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 28 | InlineAsm *InlineAsm::get(const FunctionType *Ty, StringRef AsmString, |
| 29 | StringRef Constraints, bool hasSideEffects, |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 30 | bool isAlignStack) { |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 31 | InlineAsmKeyType Key(AsmString, Constraints, hasSideEffects, isAlignStack); |
| 32 | LLVMContextImpl *pImpl = Ty->getContext().pImpl; |
| 33 | return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(Ty), Key); |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 36 | InlineAsm::InlineAsm(const PointerType *Ty, const std::string &asmString, |
| 37 | const std::string &constraints, bool hasSideEffects, |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 38 | bool isAlignStack) |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 39 | : Value(Ty, Value::InlineAsmVal), |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 40 | AsmString(asmString), |
Dale Johannesen | 1cfb958 | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 41 | Constraints(constraints), HasSideEffects(hasSideEffects), |
| 42 | IsAlignStack(isAlignStack) { |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 43 | |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 44 | // Do various checks on the constraint string and type. |
Jeffrey Yasskin | ade270e | 2010-03-21 20:37:19 +0000 | [diff] [blame] | 45 | assert(Verify(getFunctionType(), constraints) && |
| 46 | "Function type not legal for constraints!"); |
| 47 | } |
| 48 | |
| 49 | void InlineAsm::destroyConstant() { |
| 50 | delete this; |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | const FunctionType *InlineAsm::getFunctionType() const { |
| 54 | return cast<FunctionType>(getType()->getElementType()); |
| 55 | } |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 56 | |
| 57 | ///Default constructor. |
| 58 | InlineAsm::ConstraintInfo::ConstraintInfo() : |
| 59 | isMultipleAlternative(false), |
| 60 | Type(isInput), isEarlyClobber(false), |
| 61 | MatchingInput(-1), isCommutative(false), |
| 62 | isIndirect(false), currentAlternativeIndex(0) { |
| 63 | } |
| 64 | |
| 65 | /// Copy constructor. |
| 66 | InlineAsm::ConstraintInfo::ConstraintInfo(const ConstraintInfo &other) : |
| 67 | isMultipleAlternative(other.isMultipleAlternative), |
| 68 | Type(other.Type), isEarlyClobber(other.isEarlyClobber), |
| 69 | MatchingInput(other.MatchingInput), isCommutative(other.isCommutative), |
| 70 | isIndirect(other.isIndirect), Codes(other.Codes), |
| 71 | multipleAlternatives(other.multipleAlternatives), |
| 72 | currentAlternativeIndex(other.currentAlternativeIndex) { |
| 73 | } |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 75 | /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the |
| 76 | /// fields in this structure. If the constraint string is not understood, |
| 77 | /// return true, otherwise return false. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 78 | bool InlineAsm::ConstraintInfo::Parse(StringRef Str, |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame] | 79 | std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) { |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 80 | StringRef::iterator I = Str.begin(), E = Str.end(); |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 81 | unsigned multipleAlternativeCount = Str.count('|') + 1; |
| 82 | unsigned multipleAlternativeIndex = 0; |
| 83 | std::vector<std::string> *pCodes = &Codes; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 84 | |
| 85 | // Initialize |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 86 | isMultipleAlternative = (multipleAlternativeCount > 1 ? true : false); |
| 87 | if (isMultipleAlternative) { |
| 88 | multipleAlternatives.resize(multipleAlternativeCount); |
| 89 | pCodes = &multipleAlternatives[0].Codes; |
| 90 | } |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 91 | Type = isInput; |
| 92 | isEarlyClobber = false; |
Chris Lattner | 860df6e | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 93 | MatchingInput = -1; |
Chris Lattner | de5a9f4 | 2006-02-23 23:36:53 +0000 | [diff] [blame] | 94 | isCommutative = false; |
Chris Lattner | c48e2c3 | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 95 | isIndirect = false; |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 96 | currentAlternativeIndex = 0; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 97 | |
Chris Lattner | c48e2c3 | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 98 | // Parse prefixes. |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 99 | if (*I == '~') { |
| 100 | Type = isClobber; |
| 101 | ++I; |
| 102 | } else if (*I == '=') { |
| 103 | ++I; |
| 104 | Type = isOutput; |
Chris Lattner | c48e2c3 | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (*I == '*') { |
| 108 | isIndirect = true; |
| 109 | ++I; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (I == E) return true; // Just a prefix, like "==" or "~". |
| 113 | |
| 114 | // Parse the modifiers. |
| 115 | bool DoneWithModifiers = false; |
| 116 | while (!DoneWithModifiers) { |
| 117 | switch (*I) { |
| 118 | default: |
| 119 | DoneWithModifiers = true; |
| 120 | break; |
Chris Lattner | de5a9f4 | 2006-02-23 23:36:53 +0000 | [diff] [blame] | 121 | case '&': // Early clobber. |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 122 | if (Type != isOutput || // Cannot early clobber anything but output. |
| 123 | isEarlyClobber) // Reject &&&&&& |
| 124 | return true; |
| 125 | isEarlyClobber = true; |
| 126 | break; |
Chris Lattner | de5a9f4 | 2006-02-23 23:36:53 +0000 | [diff] [blame] | 127 | case '%': // Commutative. |
| 128 | if (Type == isClobber || // Cannot commute clobbers. |
| 129 | isCommutative) // Reject %%%%% |
| 130 | return true; |
| 131 | isCommutative = true; |
| 132 | break; |
| 133 | case '#': // Comment. |
| 134 | case '*': // Register preferencing. |
| 135 | return true; // Not supported. |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (!DoneWithModifiers) { |
| 139 | ++I; |
| 140 | if (I == E) return true; // Just prefixes and modifiers! |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Parse the various constraints. |
| 145 | while (I != E) { |
| 146 | if (*I == '{') { // Physical register reference. |
| 147 | // Find the end of the register name. |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 148 | StringRef::iterator ConstraintEnd = std::find(I+1, E, '}'); |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 149 | if (ConstraintEnd == E) return true; // "{foo" |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 150 | pCodes->push_back(std::string(I, ConstraintEnd+1)); |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 151 | I = ConstraintEnd+1; |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame] | 152 | } else if (isdigit(*I)) { // Matching Constraint |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 153 | // Maximal munch numbers. |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 154 | StringRef::iterator NumStart = I; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 155 | while (I != E && isdigit(*I)) |
| 156 | ++I; |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 157 | pCodes->push_back(std::string(NumStart, I)); |
| 158 | unsigned N = atoi(pCodes->back().c_str()); |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame] | 159 | // Check that this is a valid matching constraint! |
| 160 | if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput|| |
| 161 | Type != isInput) |
| 162 | return true; // Invalid constraint number. |
| 163 | |
Chris Lattner | 860df6e | 2008-10-17 16:47:46 +0000 | [diff] [blame] | 164 | // If Operand N already has a matching input, reject this. An output |
| 165 | // can't be constrained to the same value as multiple inputs. |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 166 | if (isMultipleAlternative) { |
| 167 | InlineAsm::SubConstraintInfo &scInfo = |
| 168 | ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex]; |
| 169 | if (scInfo.MatchingInput != -1) |
| 170 | return true; |
| 171 | // Note that operand #n has a matching input. |
| 172 | scInfo.MatchingInput = ConstraintsSoFar.size(); |
| 173 | } else { |
| 174 | if (ConstraintsSoFar[N].hasMatchingInput()) |
| 175 | return true; |
| 176 | // Note that operand #n has a matching input. |
| 177 | ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size(); |
| 178 | } |
| 179 | } else if (*I == '|') { |
| 180 | multipleAlternativeIndex++; |
| 181 | pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes; |
| 182 | ++I; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 183 | } else { |
| 184 | // Single letter constraint. |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 185 | pCodes->push_back(std::string(I, I+1)); |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 186 | ++I; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
John Thompson | 1094c80 | 2010-09-13 18:15:37 +0000 | [diff] [blame^] | 193 | /// selectAlternative - Point this constraint to the alternative constraint |
| 194 | /// indicated by the index. |
| 195 | void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) { |
| 196 | if (index < multipleAlternatives.size()) { |
| 197 | currentAlternativeIndex = index; |
| 198 | InlineAsm::SubConstraintInfo &scInfo = |
| 199 | multipleAlternatives[currentAlternativeIndex]; |
| 200 | MatchingInput = scInfo.MatchingInput; |
| 201 | Codes = scInfo.Codes; |
| 202 | } |
| 203 | } |
| 204 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 205 | std::vector<InlineAsm::ConstraintInfo> |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 206 | InlineAsm::ParseConstraints(StringRef Constraints) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 207 | std::vector<ConstraintInfo> Result; |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 208 | |
| 209 | // Scan the constraints string. |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 210 | for (StringRef::iterator I = Constraints.begin(), |
| 211 | E = Constraints.end(); I != E; ) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 212 | ConstraintInfo Info; |
| 213 | |
| 214 | // Find the end of this constraint. |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 215 | StringRef::iterator ConstraintEnd = std::find(I, E, ','); |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 216 | |
| 217 | if (ConstraintEnd == I || // Empty constraint like ",," |
Benjamin Kramer | 5aaf677 | 2010-07-25 23:18:32 +0000 | [diff] [blame] | 218 | Info.Parse(StringRef(I, ConstraintEnd-I), Result)) { |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame] | 219 | Result.clear(); // Erroneous constraint? |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 220 | break; |
| 221 | } |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 222 | |
| 223 | Result.push_back(Info); |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 225 | // ConstraintEnd may be either the next comma or the end of the string. In |
| 226 | // the former case, we skip the comma. |
| 227 | I = ConstraintEnd; |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 228 | if (I != E) { |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 229 | ++I; |
| 230 | if (I == E) { Result.clear(); break; } // don't allow "xyz," |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return Result; |
| 235 | } |
| 236 | |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 237 | /// Verify - Verify that the specified constraint string is reasonable for the |
| 238 | /// specified function type, and otherwise validate the constraint string. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 239 | bool InlineAsm::Verify(const FunctionType *Ty, StringRef ConstStr) { |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 240 | if (Ty->isVarArg()) return false; |
| 241 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 242 | std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr); |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 243 | |
| 244 | // Error parsing constraints. |
| 245 | if (Constraints.empty() && !ConstStr.empty()) return false; |
| 246 | |
| 247 | unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0; |
Chris Lattner | ee00d04 | 2008-05-22 04:46:38 +0000 | [diff] [blame] | 248 | unsigned NumIndirect = 0; |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 249 | |
| 250 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 251 | switch (Constraints[i].Type) { |
| 252 | case InlineAsm::isOutput: |
Chris Lattner | ee00d04 | 2008-05-22 04:46:38 +0000 | [diff] [blame] | 253 | if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0) |
| 254 | return false; // outputs before inputs and clobbers. |
Chris Lattner | c48e2c3 | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 255 | if (!Constraints[i].isIndirect) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 256 | ++NumOutputs; |
| 257 | break; |
| 258 | } |
Chris Lattner | ee00d04 | 2008-05-22 04:46:38 +0000 | [diff] [blame] | 259 | ++NumIndirect; |
Chris Lattner | c48e2c3 | 2007-04-28 01:02:58 +0000 | [diff] [blame] | 260 | // FALLTHROUGH for Indirect Outputs. |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 261 | case InlineAsm::isInput: |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 262 | if (NumClobbers) return false; // inputs before clobbers. |
| 263 | ++NumInputs; |
| 264 | break; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 265 | case InlineAsm::isClobber: |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 266 | ++NumClobbers; |
| 267 | break; |
| 268 | } |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 269 | } |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 10748d8 | 2008-04-27 23:33:55 +0000 | [diff] [blame] | 271 | switch (NumOutputs) { |
| 272 | case 0: |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 273 | if (!Ty->getReturnType()->isVoidTy()) return false; |
Chris Lattner | 10748d8 | 2008-04-27 23:33:55 +0000 | [diff] [blame] | 274 | break; |
| 275 | case 1: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 276 | if (Ty->getReturnType()->isStructTy()) return false; |
Chris Lattner | 10748d8 | 2008-04-27 23:33:55 +0000 | [diff] [blame] | 277 | break; |
| 278 | default: |
| 279 | const StructType *STy = dyn_cast<StructType>(Ty->getReturnType()); |
| 280 | if (STy == 0 || STy->getNumElements() != NumOutputs) |
| 281 | return false; |
| 282 | break; |
| 283 | } |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 284 | |
| 285 | if (Ty->getNumParams() != NumInputs) return false; |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 286 | return true; |
| 287 | } |
Reid Spencer | 5113dc5 | 2006-06-07 23:03:13 +0000 | [diff] [blame] | 288 | |