blob: 16d874f32fc39a76eb684d9a9cfe9e484822f05f [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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/InlineAsm.h"
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000015#include "ConstantsContext.h"
16#include "LLVMContextImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/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
Chris Lattner7ed31012006-02-01 01:29:47 +000067/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
68/// fields in this structure. If the constraint string is not understood,
69/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000070bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
John Thompsone8360b72010-10-29 17:29:13 +000071 InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000072 StringRef::iterator I = Str.begin(), E = Str.end();
John Thompson1094c802010-09-13 18:15:37 +000073 unsigned multipleAlternativeCount = Str.count('|') + 1;
74 unsigned multipleAlternativeIndex = 0;
John Thompsone8360b72010-10-29 17:29:13 +000075 ConstraintCodeVector *pCodes = &Codes;
Chris Lattner7ed31012006-02-01 01:29:47 +000076
77 // Initialize
John Thompson1094c802010-09-13 18:15:37 +000078 isMultipleAlternative = (multipleAlternativeCount > 1 ? true : false);
79 if (isMultipleAlternative) {
80 multipleAlternatives.resize(multipleAlternativeCount);
81 pCodes = &multipleAlternatives[0].Codes;
82 }
Chris Lattner7ed31012006-02-01 01:29:47 +000083 Type = isInput;
84 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000085 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000086 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000087 isIndirect = false;
John Thompson1094c802010-09-13 18:15:37 +000088 currentAlternativeIndex = 0;
Chris Lattner7ed31012006-02-01 01:29:47 +000089
Chris Lattnerc48e2c32007-04-28 01:02:58 +000090 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000091 if (*I == '~') {
92 Type = isClobber;
93 ++I;
Akira Hatanaka489dece2014-09-05 22:30:32 +000094
95 // '{' must immediately follow '~'.
96 if (I != E && *I != '{')
97 return true;
Chris Lattner7ed31012006-02-01 01:29:47 +000098 } else if (*I == '=') {
99 ++I;
100 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000101 }
102
103 if (*I == '*') {
104 isIndirect = true;
105 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000106 }
107
108 if (I == E) return true; // Just a prefix, like "==" or "~".
109
110 // Parse the modifiers.
111 bool DoneWithModifiers = false;
112 while (!DoneWithModifiers) {
113 switch (*I) {
114 default:
115 DoneWithModifiers = true;
116 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000117 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +0000118 if (Type != isOutput || // Cannot early clobber anything but output.
119 isEarlyClobber) // Reject &&&&&&
120 return true;
121 isEarlyClobber = true;
122 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000123 case '%': // Commutative.
124 if (Type == isClobber || // Cannot commute clobbers.
125 isCommutative) // Reject %%%%%
126 return true;
127 isCommutative = true;
128 break;
129 case '#': // Comment.
130 case '*': // Register preferencing.
131 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000132 }
133
134 if (!DoneWithModifiers) {
135 ++I;
136 if (I == E) return true; // Just prefixes and modifiers!
137 }
138 }
139
140 // Parse the various constraints.
141 while (I != E) {
142 if (*I == '{') { // Physical register reference.
143 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000144 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000145 if (ConstraintEnd == E) return true; // "{foo"
John Thompson1094c802010-09-13 18:15:37 +0000146 pCodes->push_back(std::string(I, ConstraintEnd+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000147 I = ConstraintEnd+1;
Guy Benyei83c74e92013-02-12 21:21:59 +0000148 } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000149 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000150 StringRef::iterator NumStart = I;
Guy Benyei83c74e92013-02-12 21:21:59 +0000151 while (I != E && isdigit(static_cast<unsigned char>(*I)))
Chris Lattner7ed31012006-02-01 01:29:47 +0000152 ++I;
John Thompson1094c802010-09-13 18:15:37 +0000153 pCodes->push_back(std::string(NumStart, I));
154 unsigned N = atoi(pCodes->back().c_str());
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000155 // Check that this is a valid matching constraint!
156 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
157 Type != isInput)
158 return true; // Invalid constraint number.
159
Chris Lattner860df6e2008-10-17 16:47:46 +0000160 // If Operand N already has a matching input, reject this. An output
161 // can't be constrained to the same value as multiple inputs.
John Thompson1094c802010-09-13 18:15:37 +0000162 if (isMultipleAlternative) {
163 InlineAsm::SubConstraintInfo &scInfo =
164 ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
165 if (scInfo.MatchingInput != -1)
166 return true;
167 // Note that operand #n has a matching input.
168 scInfo.MatchingInput = ConstraintsSoFar.size();
169 } else {
170 if (ConstraintsSoFar[N].hasMatchingInput())
171 return true;
172 // Note that operand #n has a matching input.
173 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
174 }
175 } else if (*I == '|') {
176 multipleAlternativeIndex++;
177 pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
178 ++I;
Eric Christopherca9b7bb2011-06-02 19:26:37 +0000179 } else if (*I == '^') {
180 // Multi-letter constraint
Eric Christopher1e3e8932011-06-03 22:09:12 +0000181 // FIXME: For now assuming these are 2-character constraints.
182 pCodes->push_back(std::string(I+1, I+3));
183 I += 3;
Chris Lattner7ed31012006-02-01 01:29:47 +0000184 } else {
185 // Single letter constraint.
John Thompson1094c802010-09-13 18:15:37 +0000186 pCodes->push_back(std::string(I, I+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000187 ++I;
188 }
189 }
190
191 return false;
192}
193
John Thompson1094c802010-09-13 18:15:37 +0000194/// selectAlternative - Point this constraint to the alternative constraint
195/// indicated by the index.
196void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
197 if (index < multipleAlternatives.size()) {
198 currentAlternativeIndex = index;
199 InlineAsm::SubConstraintInfo &scInfo =
200 multipleAlternatives[currentAlternativeIndex];
201 MatchingInput = scInfo.MatchingInput;
202 Codes = scInfo.Codes;
203 }
204}
205
John Thompsone8360b72010-10-29 17:29:13 +0000206InlineAsm::ConstraintInfoVector
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000207InlineAsm::ParseConstraints(StringRef Constraints) {
John Thompsone8360b72010-10-29 17:29:13 +0000208 ConstraintInfoVector Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000209
210 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000211 for (StringRef::iterator I = Constraints.begin(),
212 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000213 ConstraintInfo Info;
214
215 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000216 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000217
218 if (ConstraintEnd == I || // Empty constraint like ",,"
Benjamin Kramer5aaf6772010-07-25 23:18:32 +0000219 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000220 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000221 break;
222 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000223
224 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000225
Chris Lattner7ed31012006-02-01 01:29:47 +0000226 // ConstraintEnd may be either the next comma or the end of the string. In
227 // the former case, we skip the comma.
228 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000229 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000230 ++I;
231 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
232 }
233 }
234
235 return Result;
236}
237
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000238/// Verify - Verify that the specified constraint string is reasonable for the
239/// specified function type, and otherwise validate the constraint string.
Chris Lattner229907c2011-07-18 04:54:35 +0000240bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000241 if (Ty->isVarArg()) return false;
242
John Thompsone8360b72010-10-29 17:29:13 +0000243 ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000244
245 // Error parsing constraints.
246 if (Constraints.empty() && !ConstStr.empty()) return false;
247
248 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000249 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000250
251 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000252 switch (Constraints[i].Type) {
253 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000254 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
255 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000256 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000257 ++NumOutputs;
258 break;
259 }
Chris Lattneree00d042008-05-22 04:46:38 +0000260 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000261 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000262 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000263 if (NumClobbers) return false; // inputs before clobbers.
264 ++NumInputs;
265 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000266 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000267 ++NumClobbers;
268 break;
269 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000270 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000271
Chris Lattner10748d82008-04-27 23:33:55 +0000272 switch (NumOutputs) {
273 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000274 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000275 break;
276 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000277 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000278 break;
279 default:
Chris Lattner229907c2011-07-18 04:54:35 +0000280 StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
Craig Topperc6207612014-04-09 06:08:46 +0000281 if (!STy || STy->getNumElements() != NumOutputs)
Chris Lattner10748d82008-04-27 23:33:55 +0000282 return false;
283 break;
284 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000285
286 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000287 return true;
288}
Reid Spencer5113dc52006-06-07 23:03:13 +0000289