blob: 736e370a6de6249fdce9d7106071212cfa040a9b [file] [log] [blame]
Chris Lattnereef2fe72006-01-24 04:13:11 +00001//===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnereef2fe72006-01-24 04:13:11 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the InlineAsm class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/InlineAsm.h"
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000015#include "ConstantsContext.h"
16#include "LLVMContextImpl.h"
Chris Lattnereef2fe72006-01-24 04:13:11 +000017#include "llvm/DerivedTypes.h"
Jeff Cohenb24b66f2006-02-01 04:37:04 +000018#include <algorithm>
Chris Lattner8547e3a2006-01-26 00:48:33 +000019#include <cctype>
Chris Lattnereef2fe72006-01-24 04:13:11 +000020using namespace llvm;
21
Gordon Henriksen14a55692007-12-10 02:14:30 +000022// Implement the first virtual method in this class in this file so the
23// InlineAsm vtable is emitted here.
24InlineAsm::~InlineAsm() {
25}
26
27
Chris Lattner229907c2011-07-18 04:54:35 +000028InlineAsm *InlineAsm::get(FunctionType *Ty, StringRef AsmString,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000029 StringRef Constraints, bool hasSideEffects,
Dale Johannesen1cfb9582009-10-21 23:28:00 +000030 bool isAlignStack) {
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000031 InlineAsmKeyType Key(AsmString, Constraints, hasSideEffects, isAlignStack);
32 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
33 return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(Ty), Key);
Chris Lattnera2d810d2006-01-25 22:26:05 +000034}
35
Chris Lattner229907c2011-07-18 04:54:35 +000036InlineAsm::InlineAsm(PointerType *Ty, const std::string &asmString,
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000037 const std::string &constraints, bool hasSideEffects,
Dale Johannesen1cfb9582009-10-21 23:28:00 +000038 bool isAlignStack)
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000039 : Value(Ty, Value::InlineAsmVal),
Christopher Lambedf07882007-12-17 01:12:55 +000040 AsmString(asmString),
Dale Johannesen1cfb9582009-10-21 23:28:00 +000041 Constraints(constraints), HasSideEffects(hasSideEffects),
42 IsAlignStack(isAlignStack) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000043
Chris Lattnera2d810d2006-01-25 22:26:05 +000044 // Do various checks on the constraint string and type.
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000045 assert(Verify(getFunctionType(), constraints) &&
46 "Function type not legal for constraints!");
47}
48
49void InlineAsm::destroyConstant() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000050 getType()->getContext().pImpl->InlineAsms.remove(this);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000051 delete this;
Chris Lattnereef2fe72006-01-24 04:13:11 +000052}
53
Chris Lattner1d021a92011-07-15 23:15:45 +000054FunctionType *InlineAsm::getFunctionType() const {
Chris Lattnereef2fe72006-01-24 04:13:11 +000055 return cast<FunctionType>(getType()->getElementType());
56}
John Thompson1094c802010-09-13 18:15:37 +000057
58///Default constructor.
59InlineAsm::ConstraintInfo::ConstraintInfo() :
John Thompson1094c802010-09-13 18:15:37 +000060 Type(isInput), isEarlyClobber(false),
61 MatchingInput(-1), isCommutative(false),
Eric Christopher5f878342010-09-13 18:25:05 +000062 isIndirect(false), isMultipleAlternative(false),
63 currentAlternativeIndex(0) {
John Thompson1094c802010-09-13 18:15:37 +000064}
65
66/// Copy constructor.
67InlineAsm::ConstraintInfo::ConstraintInfo(const ConstraintInfo &other) :
John Thompson1094c802010-09-13 18:15:37 +000068 Type(other.Type), isEarlyClobber(other.isEarlyClobber),
69 MatchingInput(other.MatchingInput), isCommutative(other.isCommutative),
70 isIndirect(other.isIndirect), Codes(other.Codes),
Eric Christopher5f878342010-09-13 18:25:05 +000071 isMultipleAlternative(other.isMultipleAlternative),
John Thompson1094c802010-09-13 18:15:37 +000072 multipleAlternatives(other.multipleAlternatives),
73 currentAlternativeIndex(other.currentAlternativeIndex) {
74}
Chris Lattnera2d810d2006-01-25 22:26:05 +000075
Chris Lattner7ed31012006-02-01 01:29:47 +000076/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
77/// fields in this structure. If the constraint string is not understood,
78/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000079bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
John Thompsone8360b72010-10-29 17:29:13 +000080 InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000081 StringRef::iterator I = Str.begin(), E = Str.end();
John Thompson1094c802010-09-13 18:15:37 +000082 unsigned multipleAlternativeCount = Str.count('|') + 1;
83 unsigned multipleAlternativeIndex = 0;
John Thompsone8360b72010-10-29 17:29:13 +000084 ConstraintCodeVector *pCodes = &Codes;
Chris Lattner7ed31012006-02-01 01:29:47 +000085
86 // Initialize
John Thompson1094c802010-09-13 18:15:37 +000087 isMultipleAlternative = (multipleAlternativeCount > 1 ? true : false);
88 if (isMultipleAlternative) {
89 multipleAlternatives.resize(multipleAlternativeCount);
90 pCodes = &multipleAlternatives[0].Codes;
91 }
Chris Lattner7ed31012006-02-01 01:29:47 +000092 Type = isInput;
93 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000094 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000095 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000096 isIndirect = false;
John Thompson1094c802010-09-13 18:15:37 +000097 currentAlternativeIndex = 0;
Chris Lattner7ed31012006-02-01 01:29:47 +000098
Chris Lattnerc48e2c32007-04-28 01:02:58 +000099 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +0000100 if (*I == '~') {
101 Type = isClobber;
102 ++I;
103 } else if (*I == '=') {
104 ++I;
105 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000106 }
107
108 if (*I == '*') {
109 isIndirect = true;
110 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000111 }
112
113 if (I == E) return true; // Just a prefix, like "==" or "~".
114
115 // Parse the modifiers.
116 bool DoneWithModifiers = false;
117 while (!DoneWithModifiers) {
118 switch (*I) {
119 default:
120 DoneWithModifiers = true;
121 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000122 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +0000123 if (Type != isOutput || // Cannot early clobber anything but output.
124 isEarlyClobber) // Reject &&&&&&
125 return true;
126 isEarlyClobber = true;
127 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000128 case '%': // Commutative.
129 if (Type == isClobber || // Cannot commute clobbers.
130 isCommutative) // Reject %%%%%
131 return true;
132 isCommutative = true;
133 break;
134 case '#': // Comment.
135 case '*': // Register preferencing.
136 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000137 }
138
139 if (!DoneWithModifiers) {
140 ++I;
141 if (I == E) return true; // Just prefixes and modifiers!
142 }
143 }
144
145 // Parse the various constraints.
146 while (I != E) {
147 if (*I == '{') { // Physical register reference.
148 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000149 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000150 if (ConstraintEnd == E) return true; // "{foo"
John Thompson1094c802010-09-13 18:15:37 +0000151 pCodes->push_back(std::string(I, ConstraintEnd+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000152 I = ConstraintEnd+1;
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000153 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000154 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000155 StringRef::iterator NumStart = I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000156 while (I != E && isdigit(*I))
157 ++I;
John Thompson1094c802010-09-13 18:15:37 +0000158 pCodes->push_back(std::string(NumStart, I));
159 unsigned N = atoi(pCodes->back().c_str());
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000160 // Check that this is a valid matching constraint!
161 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
162 Type != isInput)
163 return true; // Invalid constraint number.
164
Chris Lattner860df6e2008-10-17 16:47:46 +0000165 // If Operand N already has a matching input, reject this. An output
166 // can't be constrained to the same value as multiple inputs.
John Thompson1094c802010-09-13 18:15:37 +0000167 if (isMultipleAlternative) {
168 InlineAsm::SubConstraintInfo &scInfo =
169 ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
170 if (scInfo.MatchingInput != -1)
171 return true;
172 // Note that operand #n has a matching input.
173 scInfo.MatchingInput = ConstraintsSoFar.size();
174 } else {
175 if (ConstraintsSoFar[N].hasMatchingInput())
176 return true;
177 // Note that operand #n has a matching input.
178 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
179 }
180 } else if (*I == '|') {
181 multipleAlternativeIndex++;
182 pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
183 ++I;
Eric Christopherca9b7bb2011-06-02 19:26:37 +0000184 } else if (*I == '^') {
185 // Multi-letter constraint
Eric Christopher1e3e8932011-06-03 22:09:12 +0000186 // FIXME: For now assuming these are 2-character constraints.
187 pCodes->push_back(std::string(I+1, I+3));
188 I += 3;
Chris Lattner7ed31012006-02-01 01:29:47 +0000189 } else {
190 // Single letter constraint.
John Thompson1094c802010-09-13 18:15:37 +0000191 pCodes->push_back(std::string(I, I+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000192 ++I;
193 }
194 }
195
196 return false;
197}
198
John Thompson1094c802010-09-13 18:15:37 +0000199/// selectAlternative - Point this constraint to the alternative constraint
200/// indicated by the index.
201void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
202 if (index < multipleAlternatives.size()) {
203 currentAlternativeIndex = index;
204 InlineAsm::SubConstraintInfo &scInfo =
205 multipleAlternatives[currentAlternativeIndex];
206 MatchingInput = scInfo.MatchingInput;
207 Codes = scInfo.Codes;
208 }
209}
210
John Thompsone8360b72010-10-29 17:29:13 +0000211InlineAsm::ConstraintInfoVector
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000212InlineAsm::ParseConstraints(StringRef Constraints) {
John Thompsone8360b72010-10-29 17:29:13 +0000213 ConstraintInfoVector Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000214
215 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000216 for (StringRef::iterator I = Constraints.begin(),
217 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000218 ConstraintInfo Info;
219
220 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000221 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000222
223 if (ConstraintEnd == I || // Empty constraint like ",,"
Benjamin Kramer5aaf6772010-07-25 23:18:32 +0000224 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000225 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000226 break;
227 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000228
229 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000230
Chris Lattner7ed31012006-02-01 01:29:47 +0000231 // ConstraintEnd may be either the next comma or the end of the string. In
232 // the former case, we skip the comma.
233 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000234 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000235 ++I;
236 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
237 }
238 }
239
240 return Result;
241}
242
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000243/// Verify - Verify that the specified constraint string is reasonable for the
244/// specified function type, and otherwise validate the constraint string.
Chris Lattner229907c2011-07-18 04:54:35 +0000245bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000246 if (Ty->isVarArg()) return false;
247
John Thompsone8360b72010-10-29 17:29:13 +0000248 ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000249
250 // Error parsing constraints.
251 if (Constraints.empty() && !ConstStr.empty()) return false;
252
253 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000254 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000255
256 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000257 switch (Constraints[i].Type) {
258 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000259 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
260 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000261 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000262 ++NumOutputs;
263 break;
264 }
Chris Lattneree00d042008-05-22 04:46:38 +0000265 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000266 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000267 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000268 if (NumClobbers) return false; // inputs before clobbers.
269 ++NumInputs;
270 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000271 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000272 ++NumClobbers;
273 break;
274 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000275 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000276
Chris Lattner10748d82008-04-27 23:33:55 +0000277 switch (NumOutputs) {
278 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000279 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000280 break;
281 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000282 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000283 break;
284 default:
Chris Lattner229907c2011-07-18 04:54:35 +0000285 StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
Chris Lattner10748d82008-04-27 23:33:55 +0000286 if (STy == 0 || STy->getNumElements() != NumOutputs)
287 return false;
288 break;
289 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000290
291 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000292 return true;
293}
Reid Spencer5113dc52006-06-07 23:03:13 +0000294