blob: 2e636aacfde889f1aea2c6c14ddf2f74e90aedea [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,
Chad Rosierd8c76102012-09-05 19:00:49 +000030 bool isAlignStack, AsmDialect asmDialect) {
Chad Rosier8b3014e2012-09-04 22:46:24 +000031 InlineAsmKeyType Key(AsmString, Constraints, hasSideEffects, isAlignStack,
32 asmDialect);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000033 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
34 return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(Ty), Key);
Chris Lattnera2d810d2006-01-25 22:26:05 +000035}
36
Chris Lattner229907c2011-07-18 04:54:35 +000037InlineAsm::InlineAsm(PointerType *Ty, const std::string &asmString,
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000038 const std::string &constraints, bool hasSideEffects,
Chad Rosierd8c76102012-09-05 19:00:49 +000039 bool isAlignStack, AsmDialect asmDialect)
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000040 : Value(Ty, Value::InlineAsmVal),
Chad Rosier8b3014e2012-09-04 22:46:24 +000041 AsmString(asmString), Constraints(constraints),
42 HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
Chad Rosierd8c76102012-09-05 19:00:49 +000043 Dialect(asmDialect) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000044
Chris Lattnera2d810d2006-01-25 22:26:05 +000045 // Do various checks on the constraint string and type.
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000046 assert(Verify(getFunctionType(), constraints) &&
47 "Function type not legal for constraints!");
48}
49
50void InlineAsm::destroyConstant() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000051 getType()->getContext().pImpl->InlineAsms.remove(this);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000052 delete this;
Chris Lattnereef2fe72006-01-24 04:13:11 +000053}
54
Chris Lattner1d021a92011-07-15 23:15:45 +000055FunctionType *InlineAsm::getFunctionType() const {
Chris Lattnereef2fe72006-01-24 04:13:11 +000056 return cast<FunctionType>(getType()->getElementType());
57}
John Thompson1094c802010-09-13 18:15:37 +000058
59///Default constructor.
60InlineAsm::ConstraintInfo::ConstraintInfo() :
John Thompson1094c802010-09-13 18:15:37 +000061 Type(isInput), isEarlyClobber(false),
62 MatchingInput(-1), isCommutative(false),
Eric Christopher5f878342010-09-13 18:25:05 +000063 isIndirect(false), isMultipleAlternative(false),
64 currentAlternativeIndex(0) {
John Thompson1094c802010-09-13 18:15:37 +000065}
66
67/// Copy constructor.
68InlineAsm::ConstraintInfo::ConstraintInfo(const ConstraintInfo &other) :
John Thompson1094c802010-09-13 18:15:37 +000069 Type(other.Type), isEarlyClobber(other.isEarlyClobber),
70 MatchingInput(other.MatchingInput), isCommutative(other.isCommutative),
71 isIndirect(other.isIndirect), Codes(other.Codes),
Eric Christopher5f878342010-09-13 18:25:05 +000072 isMultipleAlternative(other.isMultipleAlternative),
John Thompson1094c802010-09-13 18:15:37 +000073 multipleAlternatives(other.multipleAlternatives),
74 currentAlternativeIndex(other.currentAlternativeIndex) {
75}
Chris Lattnera2d810d2006-01-25 22:26:05 +000076
Chris Lattner7ed31012006-02-01 01:29:47 +000077/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
78/// fields in this structure. If the constraint string is not understood,
79/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000080bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
John Thompsone8360b72010-10-29 17:29:13 +000081 InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000082 StringRef::iterator I = Str.begin(), E = Str.end();
John Thompson1094c802010-09-13 18:15:37 +000083 unsigned multipleAlternativeCount = Str.count('|') + 1;
84 unsigned multipleAlternativeIndex = 0;
John Thompsone8360b72010-10-29 17:29:13 +000085 ConstraintCodeVector *pCodes = &Codes;
Chris Lattner7ed31012006-02-01 01:29:47 +000086
87 // Initialize
John Thompson1094c802010-09-13 18:15:37 +000088 isMultipleAlternative = (multipleAlternativeCount > 1 ? true : false);
89 if (isMultipleAlternative) {
90 multipleAlternatives.resize(multipleAlternativeCount);
91 pCodes = &multipleAlternatives[0].Codes;
92 }
Chris Lattner7ed31012006-02-01 01:29:47 +000093 Type = isInput;
94 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000095 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000096 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000097 isIndirect = false;
John Thompson1094c802010-09-13 18:15:37 +000098 currentAlternativeIndex = 0;
Chris Lattner7ed31012006-02-01 01:29:47 +000099
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000100 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +0000101 if (*I == '~') {
102 Type = isClobber;
103 ++I;
104 } else if (*I == '=') {
105 ++I;
106 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000107 }
108
109 if (*I == '*') {
110 isIndirect = true;
111 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000112 }
113
114 if (I == E) return true; // Just a prefix, like "==" or "~".
115
116 // Parse the modifiers.
117 bool DoneWithModifiers = false;
118 while (!DoneWithModifiers) {
119 switch (*I) {
120 default:
121 DoneWithModifiers = true;
122 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000123 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +0000124 if (Type != isOutput || // Cannot early clobber anything but output.
125 isEarlyClobber) // Reject &&&&&&
126 return true;
127 isEarlyClobber = true;
128 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000129 case '%': // Commutative.
130 if (Type == isClobber || // Cannot commute clobbers.
131 isCommutative) // Reject %%%%%
132 return true;
133 isCommutative = true;
134 break;
135 case '#': // Comment.
136 case '*': // Register preferencing.
137 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000138 }
139
140 if (!DoneWithModifiers) {
141 ++I;
142 if (I == E) return true; // Just prefixes and modifiers!
143 }
144 }
145
146 // Parse the various constraints.
147 while (I != E) {
148 if (*I == '{') { // Physical register reference.
149 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000150 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000151 if (ConstraintEnd == E) return true; // "{foo"
John Thompson1094c802010-09-13 18:15:37 +0000152 pCodes->push_back(std::string(I, ConstraintEnd+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000153 I = ConstraintEnd+1;
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000154 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000155 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000156 StringRef::iterator NumStart = I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000157 while (I != E && isdigit(*I))
158 ++I;
John Thompson1094c802010-09-13 18:15:37 +0000159 pCodes->push_back(std::string(NumStart, I));
160 unsigned N = atoi(pCodes->back().c_str());
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000161 // Check that this is a valid matching constraint!
162 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
163 Type != isInput)
164 return true; // Invalid constraint number.
165
Chris Lattner860df6e2008-10-17 16:47:46 +0000166 // If Operand N already has a matching input, reject this. An output
167 // can't be constrained to the same value as multiple inputs.
John Thompson1094c802010-09-13 18:15:37 +0000168 if (isMultipleAlternative) {
169 InlineAsm::SubConstraintInfo &scInfo =
170 ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
171 if (scInfo.MatchingInput != -1)
172 return true;
173 // Note that operand #n has a matching input.
174 scInfo.MatchingInput = ConstraintsSoFar.size();
175 } else {
176 if (ConstraintsSoFar[N].hasMatchingInput())
177 return true;
178 // Note that operand #n has a matching input.
179 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
180 }
181 } else if (*I == '|') {
182 multipleAlternativeIndex++;
183 pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
184 ++I;
Eric Christopherca9b7bb2011-06-02 19:26:37 +0000185 } else if (*I == '^') {
186 // Multi-letter constraint
Eric Christopher1e3e8932011-06-03 22:09:12 +0000187 // FIXME: For now assuming these are 2-character constraints.
188 pCodes->push_back(std::string(I+1, I+3));
189 I += 3;
Chris Lattner7ed31012006-02-01 01:29:47 +0000190 } else {
191 // Single letter constraint.
John Thompson1094c802010-09-13 18:15:37 +0000192 pCodes->push_back(std::string(I, I+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000193 ++I;
194 }
195 }
196
197 return false;
198}
199
John Thompson1094c802010-09-13 18:15:37 +0000200/// selectAlternative - Point this constraint to the alternative constraint
201/// indicated by the index.
202void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
203 if (index < multipleAlternatives.size()) {
204 currentAlternativeIndex = index;
205 InlineAsm::SubConstraintInfo &scInfo =
206 multipleAlternatives[currentAlternativeIndex];
207 MatchingInput = scInfo.MatchingInput;
208 Codes = scInfo.Codes;
209 }
210}
211
John Thompsone8360b72010-10-29 17:29:13 +0000212InlineAsm::ConstraintInfoVector
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000213InlineAsm::ParseConstraints(StringRef Constraints) {
John Thompsone8360b72010-10-29 17:29:13 +0000214 ConstraintInfoVector Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000215
216 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000217 for (StringRef::iterator I = Constraints.begin(),
218 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000219 ConstraintInfo Info;
220
221 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000222 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000223
224 if (ConstraintEnd == I || // Empty constraint like ",,"
Benjamin Kramer5aaf6772010-07-25 23:18:32 +0000225 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000226 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000227 break;
228 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000229
230 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000231
Chris Lattner7ed31012006-02-01 01:29:47 +0000232 // ConstraintEnd may be either the next comma or the end of the string. In
233 // the former case, we skip the comma.
234 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000235 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000236 ++I;
237 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
238 }
239 }
240
241 return Result;
242}
243
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000244/// Verify - Verify that the specified constraint string is reasonable for the
245/// specified function type, and otherwise validate the constraint string.
Chris Lattner229907c2011-07-18 04:54:35 +0000246bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000247 if (Ty->isVarArg()) return false;
248
John Thompsone8360b72010-10-29 17:29:13 +0000249 ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000250
251 // Error parsing constraints.
252 if (Constraints.empty() && !ConstStr.empty()) return false;
253
254 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000255 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000256
257 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000258 switch (Constraints[i].Type) {
259 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000260 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
261 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000262 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000263 ++NumOutputs;
264 break;
265 }
Chris Lattneree00d042008-05-22 04:46:38 +0000266 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000267 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000268 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000269 if (NumClobbers) return false; // inputs before clobbers.
270 ++NumInputs;
271 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000272 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000273 ++NumClobbers;
274 break;
275 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000276 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000277
Chris Lattner10748d82008-04-27 23:33:55 +0000278 switch (NumOutputs) {
279 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000280 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000281 break;
282 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000283 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000284 break;
285 default:
Chris Lattner229907c2011-07-18 04:54:35 +0000286 StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
Chris Lattner10748d82008-04-27 23:33:55 +0000287 if (STy == 0 || STy->getNumElements() != NumOutputs)
288 return false;
289 break;
290 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000291
292 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000293 return true;
294}
Reid Spencer5113dc52006-06-07 23:03:13 +0000295