blob: 8667d7aab583863c4ff7e194fe081670f9bb3d7b [file] [log] [blame]
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001//===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//
Chris Lattnereef2fe72006-01-24 04:13:11 +00002//
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 Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/IR/InlineAsm.h"
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000015#include "ConstantsContext.h"
16#include "LLVMContextImpl.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000017#include "llvm/ADT/StringRef.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000019#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/Value.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/Compiler.h"
Jeff Cohenb24b66f2006-02-01 04:37:04 +000023#include <algorithm>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000024#include <cassert>
Chris Lattner8547e3a2006-01-26 00:48:33 +000025#include <cctype>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000026#include <cstddef>
27#include <cstdlib>
28
Chris Lattnereef2fe72006-01-24 04:13:11 +000029using namespace llvm;
30
David Blaikie71c9c9c2015-07-28 00:06:38 +000031InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000032 const std::string &constraints, bool hasSideEffects,
Chad Rosierd8c76102012-09-05 19:00:49 +000033 bool isAlignStack, AsmDialect asmDialect)
David Blaikie71c9c9c2015-07-28 00:06:38 +000034 : Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),
35 AsmString(asmString), Constraints(constraints), FTy(FTy),
36 HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
37 Dialect(asmDialect) {
Chris Lattnera2d810d2006-01-25 22:26:05 +000038 // Do various checks on the constraint string and type.
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000039 assert(Verify(getFunctionType(), constraints) &&
40 "Function type not legal for constraints!");
41}
42
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000043InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,
44 StringRef Constraints, bool hasSideEffects,
45 bool isAlignStack, AsmDialect asmDialect) {
46 InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,
47 isAlignStack, asmDialect);
48 LLVMContextImpl *pImpl = FTy->getContext().pImpl;
49 return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);
50}
51
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000052void InlineAsm::destroyConstant() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000053 getType()->getContext().pImpl->InlineAsms.remove(this);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000054 delete this;
Chris Lattnereef2fe72006-01-24 04:13:11 +000055}
56
Chris Lattner1d021a92011-07-15 23:15:45 +000057FunctionType *InlineAsm::getFunctionType() const {
David Blaikie71c9c9c2015-07-28 00:06:38 +000058 return FTy;
Chris Lattnereef2fe72006-01-24 04:13:11 +000059}
John Thompson1094c802010-09-13 18:15:37 +000060
Chris Lattner7ed31012006-02-01 01:29:47 +000061/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
62/// fields in this structure. If the constraint string is not understood,
63/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000064bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
John Thompsone8360b72010-10-29 17:29:13 +000065 InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000066 StringRef::iterator I = Str.begin(), E = Str.end();
John Thompson1094c802010-09-13 18:15:37 +000067 unsigned multipleAlternativeCount = Str.count('|') + 1;
68 unsigned multipleAlternativeIndex = 0;
John Thompsone8360b72010-10-29 17:29:13 +000069 ConstraintCodeVector *pCodes = &Codes;
Eric Christopherf3e79e82015-02-10 21:15:06 +000070
Chris Lattner7ed31012006-02-01 01:29:47 +000071 // Initialize
David Blaikiedc3f01e2015-03-09 01:57:13 +000072 isMultipleAlternative = multipleAlternativeCount > 1;
John Thompson1094c802010-09-13 18:15:37 +000073 if (isMultipleAlternative) {
74 multipleAlternatives.resize(multipleAlternativeCount);
75 pCodes = &multipleAlternatives[0].Codes;
76 }
Chris Lattner7ed31012006-02-01 01:29:47 +000077 Type = isInput;
78 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000079 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000080 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000081 isIndirect = false;
John Thompson1094c802010-09-13 18:15:37 +000082 currentAlternativeIndex = 0;
Chris Lattner7ed31012006-02-01 01:29:47 +000083
Chris Lattnerc48e2c32007-04-28 01:02:58 +000084 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000085 if (*I == '~') {
86 Type = isClobber;
87 ++I;
Akira Hatanaka489dece2014-09-05 22:30:32 +000088
89 // '{' must immediately follow '~'.
90 if (I != E && *I != '{')
91 return true;
Chris Lattner7ed31012006-02-01 01:29:47 +000092 } else if (*I == '=') {
93 ++I;
94 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000095 }
Eric Christopherf3e79e82015-02-10 21:15:06 +000096
Chris Lattnerc48e2c32007-04-28 01:02:58 +000097 if (*I == '*') {
98 isIndirect = true;
99 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000100 }
Eric Christopherf3e79e82015-02-10 21:15:06 +0000101
Chris Lattner7ed31012006-02-01 01:29:47 +0000102 if (I == E) return true; // Just a prefix, like "==" or "~".
103
104 // Parse the modifiers.
105 bool DoneWithModifiers = false;
106 while (!DoneWithModifiers) {
107 switch (*I) {
108 default:
109 DoneWithModifiers = true;
110 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000111 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +0000112 if (Type != isOutput || // Cannot early clobber anything but output.
113 isEarlyClobber) // Reject &&&&&&
114 return true;
115 isEarlyClobber = true;
116 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000117 case '%': // Commutative.
118 if (Type == isClobber || // Cannot commute clobbers.
119 isCommutative) // Reject %%%%%
120 return true;
121 isCommutative = true;
122 break;
123 case '#': // Comment.
124 case '*': // Register preferencing.
125 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000126 }
127
128 if (!DoneWithModifiers) {
129 ++I;
130 if (I == E) return true; // Just prefixes and modifiers!
131 }
132 }
133
134 // Parse the various constraints.
135 while (I != E) {
136 if (*I == '{') { // Physical register reference.
137 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000138 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000139 if (ConstraintEnd == E) return true; // "{foo"
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000140 pCodes->push_back(StringRef(I, ConstraintEnd+1 - I));
Chris Lattner7ed31012006-02-01 01:29:47 +0000141 I = ConstraintEnd+1;
Guy Benyei83c74e92013-02-12 21:21:59 +0000142 } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000143 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000144 StringRef::iterator NumStart = I;
Guy Benyei83c74e92013-02-12 21:21:59 +0000145 while (I != E && isdigit(static_cast<unsigned char>(*I)))
Chris Lattner7ed31012006-02-01 01:29:47 +0000146 ++I;
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000147 pCodes->push_back(StringRef(NumStart, I - NumStart));
John Thompson1094c802010-09-13 18:15:37 +0000148 unsigned N = atoi(pCodes->back().c_str());
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000149 // Check that this is a valid matching constraint!
150 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
151 Type != isInput)
152 return true; // Invalid constraint number.
153
Chris Lattner860df6e2008-10-17 16:47:46 +0000154 // If Operand N already has a matching input, reject this. An output
155 // can't be constrained to the same value as multiple inputs.
John Thompson1094c802010-09-13 18:15:37 +0000156 if (isMultipleAlternative) {
Karl Schimpff04a5d52015-09-03 15:41:37 +0000157 if (multipleAlternativeIndex >=
158 ConstraintsSoFar[N].multipleAlternatives.size())
Karl Schimpf388fd5a2015-09-03 15:41:34 +0000159 return true;
John Thompson1094c802010-09-13 18:15:37 +0000160 InlineAsm::SubConstraintInfo &scInfo =
161 ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
162 if (scInfo.MatchingInput != -1)
163 return true;
164 // Note that operand #n has a matching input.
165 scInfo.MatchingInput = ConstraintsSoFar.size();
Daniil Fukalov2bfbadc2017-10-25 12:51:32 +0000166 assert(scInfo.MatchingInput >= 0);
John Thompson1094c802010-09-13 18:15:37 +0000167 } else {
Benjamin Kramer9de151e2015-03-29 20:33:07 +0000168 if (ConstraintsSoFar[N].hasMatchingInput() &&
Benjamin Kramer27395712015-03-29 20:49:03 +0000169 (size_t)ConstraintsSoFar[N].MatchingInput !=
170 ConstraintsSoFar.size())
John Thompson1094c802010-09-13 18:15:37 +0000171 return true;
172 // Note that operand #n has a matching input.
173 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
Daniil Fukalov2bfbadc2017-10-25 12:51:32 +0000174 assert(ConstraintsSoFar[N].MatchingInput >= 0);
John Thompson1094c802010-09-13 18:15:37 +0000175 }
176 } else if (*I == '|') {
177 multipleAlternativeIndex++;
178 pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
179 ++I;
Eric Christopherca9b7bb2011-06-02 19:26:37 +0000180 } else if (*I == '^') {
181 // Multi-letter constraint
Eric Christopher1e3e8932011-06-03 22:09:12 +0000182 // FIXME: For now assuming these are 2-character constraints.
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000183 pCodes->push_back(StringRef(I+1, 2));
Eric Christopher1e3e8932011-06-03 22:09:12 +0000184 I += 3;
Chris Lattner7ed31012006-02-01 01:29:47 +0000185 } else {
186 // Single letter constraint.
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000187 pCodes->push_back(StringRef(I, 1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000188 ++I;
189 }
190 }
191
192 return false;
193}
194
John Thompson1094c802010-09-13 18:15:37 +0000195/// selectAlternative - Point this constraint to the alternative constraint
196/// indicated by the index.
197void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
198 if (index < multipleAlternatives.size()) {
199 currentAlternativeIndex = index;
200 InlineAsm::SubConstraintInfo &scInfo =
201 multipleAlternatives[currentAlternativeIndex];
202 MatchingInput = scInfo.MatchingInput;
203 Codes = scInfo.Codes;
204 }
205}
206
John Thompsone8360b72010-10-29 17:29:13 +0000207InlineAsm::ConstraintInfoVector
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000208InlineAsm::ParseConstraints(StringRef Constraints) {
John Thompsone8360b72010-10-29 17:29:13 +0000209 ConstraintInfoVector Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000210
211 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000212 for (StringRef::iterator I = Constraints.begin(),
213 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000214 ConstraintInfo Info;
215
216 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000217 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000218
219 if (ConstraintEnd == I || // Empty constraint like ",,"
Benjamin Kramer5aaf6772010-07-25 23:18:32 +0000220 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000221 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000222 break;
223 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000224
225 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000226
Chris Lattner7ed31012006-02-01 01:29:47 +0000227 // ConstraintEnd may be either the next comma or the end of the string. In
228 // the former case, we skip the comma.
229 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000230 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000231 ++I;
Eric Christopherf3e79e82015-02-10 21:15:06 +0000232 if (I == E) {
233 Result.clear();
234 break;
235 } // don't allow "xyz,"
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000236 }
237 }
238
239 return Result;
240}
241
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000242/// Verify - Verify that the specified constraint string is reasonable for the
243/// specified function type, and otherwise validate the constraint string.
Chris Lattner229907c2011-07-18 04:54:35 +0000244bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000245 if (Ty->isVarArg()) return false;
246
John Thompsone8360b72010-10-29 17:29:13 +0000247 ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000248
249 // Error parsing constraints.
250 if (Constraints.empty() && !ConstStr.empty()) return false;
251
252 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000253 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000254
255 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000256 switch (Constraints[i].Type) {
257 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000258 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
259 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000260 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000261 ++NumOutputs;
262 break;
263 }
Chris Lattneree00d042008-05-22 04:46:38 +0000264 ++NumIndirect;
Justin Bognerb03fd122016-08-17 05:10:15 +0000265 LLVM_FALLTHROUGH; // We fall through for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000266 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000267 if (NumClobbers) return false; // inputs before clobbers.
268 ++NumInputs;
269 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000270 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000271 ++NumClobbers;
272 break;
273 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000274 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000275
Chris Lattner10748d82008-04-27 23:33:55 +0000276 switch (NumOutputs) {
277 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000278 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000279 break;
280 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000281 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000282 break;
283 default:
Chris Lattner229907c2011-07-18 04:54:35 +0000284 StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
Craig Topperc6207612014-04-09 06:08:46 +0000285 if (!STy || STy->getNumElements() != NumOutputs)
Chris Lattner10748d82008-04-27 23:33:55 +0000286 return false;
287 break;
288 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000289
290 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000291 return true;
292}