blob: 4623f69bd9a36d5c5906005917f9da14888cdf8c [file] [log] [blame]
Eugene Zelenkob1df7872017-02-17 00:00:09 +00001//===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//
Chris Lattnercc041ba2006-01-24 04:13:11 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattnercc041ba2006-01-24 04:13:11 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the InlineAsm class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthe3e43d92017-06-06 11:49:48 +000014#include "llvm/IR/InlineAsm.h"
Jeffrey Yasskinbf48a9b2010-03-21 20:37:19 +000015#include "ConstantsContext.h"
16#include "LLVMContextImpl.h"
Eugene Zelenkob1df7872017-02-17 00:00:09 +000017#include "llvm/ADT/StringRef.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkob1df7872017-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 Cohen09f0bd32006-02-01 04:37:04 +000023#include <algorithm>
Eugene Zelenkob1df7872017-02-17 00:00:09 +000024#include <cassert>
Chris Lattner3b917782006-01-26 00:48:33 +000025#include <cctype>
Eugene Zelenkob1df7872017-02-17 00:00:09 +000026#include <cstddef>
27#include <cstdlib>
28
Chris Lattnercc041ba2006-01-24 04:13:11 +000029using namespace llvm;
30
David Blaikie04180d02015-07-28 00:06:38 +000031InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
Jeffrey Yasskinbf48a9b2010-03-21 20:37:19 +000032 const std::string &constraints, bool hasSideEffects,
Chad Rosier581600b2012-09-05 19:00:49 +000033 bool isAlignStack, AsmDialect asmDialect)
David Blaikie04180d02015-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 Lattner80cd1152006-01-25 22:26:05 +000038 // Do various checks on the constraint string and type.
Jeffrey Yasskinbf48a9b2010-03-21 20:37:19 +000039 assert(Verify(getFunctionType(), constraints) &&
40 "Function type not legal for constraints!");
41}
42
Eugene Zelenkob1df7872017-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 Yasskinbf48a9b2010-03-21 20:37:19 +000052void InlineAsm::destroyConstant() {
Chris Lattner1afcace2011-07-09 17:41:24 +000053 getType()->getContext().pImpl->InlineAsms.remove(this);
Jeffrey Yasskinbf48a9b2010-03-21 20:37:19 +000054 delete this;
Chris Lattnercc041ba2006-01-24 04:13:11 +000055}
56
Chris Lattnerc1d414a2011-07-15 23:15:45 +000057FunctionType *InlineAsm::getFunctionType() const {
David Blaikie04180d02015-07-28 00:06:38 +000058 return FTy;
Chris Lattnercc041ba2006-01-24 04:13:11 +000059}
Fangrui Songaf7b1832018-07-30 19:41:25 +000060
Chris Lattnera55079a2006-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 Dunbar2928c832009-11-06 10:58:06 +000064bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
John Thompson44ab89e2010-10-29 17:29:13 +000065 InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
Daniel Dunbar92ccf702009-07-25 06:02:13 +000066 StringRef::iterator I = Str.begin(), E = Str.end();
John Thompsoneac6e1d2010-09-13 18:15:37 +000067 unsigned multipleAlternativeCount = Str.count('|') + 1;
68 unsigned multipleAlternativeIndex = 0;
John Thompson44ab89e2010-10-29 17:29:13 +000069 ConstraintCodeVector *pCodes = &Codes;
Eric Christopherbeded8f2015-02-10 21:15:06 +000070
Chris Lattnera55079a2006-02-01 01:29:47 +000071 // Initialize
David Blaikieda4471d2015-03-09 01:57:13 +000072 isMultipleAlternative = multipleAlternativeCount > 1;
John Thompsoneac6e1d2010-09-13 18:15:37 +000073 if (isMultipleAlternative) {
74 multipleAlternatives.resize(multipleAlternativeCount);
75 pCodes = &multipleAlternatives[0].Codes;
76 }
Chris Lattnera55079a2006-02-01 01:29:47 +000077 Type = isInput;
78 isEarlyClobber = false;
Chris Lattner6bdcda32008-10-17 16:47:46 +000079 MatchingInput = -1;
Chris Lattnerfe3db462006-02-23 23:36:53 +000080 isCommutative = false;
Chris Lattner73d0d0d2007-04-28 01:02:58 +000081 isIndirect = false;
John Thompsoneac6e1d2010-09-13 18:15:37 +000082 currentAlternativeIndex = 0;
Fangrui Songaf7b1832018-07-30 19:41:25 +000083
Chris Lattner73d0d0d2007-04-28 01:02:58 +000084 // Parse prefixes.
Chris Lattnera55079a2006-02-01 01:29:47 +000085 if (*I == '~') {
86 Type = isClobber;
87 ++I;
Akira Hatanakacbbae7f2014-09-05 22:30:32 +000088
89 // '{' must immediately follow '~'.
90 if (I != E && *I != '{')
91 return true;
Chris Lattnera55079a2006-02-01 01:29:47 +000092 } else if (*I == '=') {
93 ++I;
94 Type = isOutput;
Chris Lattner73d0d0d2007-04-28 01:02:58 +000095 }
Eric Christopherbeded8f2015-02-10 21:15:06 +000096
Chris Lattner73d0d0d2007-04-28 01:02:58 +000097 if (*I == '*') {
98 isIndirect = true;
99 ++I;
Chris Lattnera55079a2006-02-01 01:29:47 +0000100 }
Eric Christopherbeded8f2015-02-10 21:15:06 +0000101
Chris Lattnera55079a2006-02-01 01:29:47 +0000102 if (I == E) return true; // Just a prefix, like "==" or "~".
Fangrui Songaf7b1832018-07-30 19:41:25 +0000103
Chris Lattnera55079a2006-02-01 01:29:47 +0000104 // Parse the modifiers.
105 bool DoneWithModifiers = false;
106 while (!DoneWithModifiers) {
107 switch (*I) {
108 default:
109 DoneWithModifiers = true;
110 break;
Chris Lattnerfe3db462006-02-23 23:36:53 +0000111 case '&': // Early clobber.
Chris Lattnera55079a2006-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 Lattnerfe3db462006-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 Lattnera55079a2006-02-01 01:29:47 +0000126 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000127
Chris Lattnera55079a2006-02-01 01:29:47 +0000128 if (!DoneWithModifiers) {
129 ++I;
130 if (I == E) return true; // Just prefixes and modifiers!
131 }
132 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000133
Chris Lattnera55079a2006-02-01 01:29:47 +0000134 // Parse the various constraints.
135 while (I != E) {
136 if (*I == '{') { // Physical register reference.
137 // Find the end of the register name.
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000138 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattnera55079a2006-02-01 01:29:47 +0000139 if (ConstraintEnd == E) return true; // "{foo"
Alexander Potapenko1c9f2a92016-05-23 13:58:04 +0000140 pCodes->push_back(StringRef(I, ConstraintEnd+1 - I));
Chris Lattnera55079a2006-02-01 01:29:47 +0000141 I = ConstraintEnd+1;
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000142 } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
Chris Lattnera55079a2006-02-01 01:29:47 +0000143 // Maximal munch numbers.
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000144 StringRef::iterator NumStart = I;
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000145 while (I != E && isdigit(static_cast<unsigned char>(*I)))
Chris Lattnera55079a2006-02-01 01:29:47 +0000146 ++I;
Alexander Potapenko1c9f2a92016-05-23 13:58:04 +0000147 pCodes->push_back(StringRef(NumStart, I - NumStart));
John Thompsoneac6e1d2010-09-13 18:15:37 +0000148 unsigned N = atoi(pCodes->back().c_str());
Chris Lattner2f0eec62006-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.
Fangrui Songaf7b1832018-07-30 19:41:25 +0000153
Chris Lattner6bdcda32008-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 Thompsoneac6e1d2010-09-13 18:15:37 +0000156 if (isMultipleAlternative) {
Karl Schimpf06bd94c2015-09-03 15:41:37 +0000157 if (multipleAlternativeIndex >=
158 ConstraintsSoFar[N].multipleAlternatives.size())
Karl Schimpfc82ddc22015-09-03 15:41:34 +0000159 return true;
John Thompsoneac6e1d2010-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 Fukalov65f17352017-10-25 12:51:32 +0000166 assert(scInfo.MatchingInput >= 0);
John Thompsoneac6e1d2010-09-13 18:15:37 +0000167 } else {
Benjamin Kramer15532872015-03-29 20:33:07 +0000168 if (ConstraintsSoFar[N].hasMatchingInput() &&
Benjamin Kramer19d05882015-03-29 20:49:03 +0000169 (size_t)ConstraintsSoFar[N].MatchingInput !=
170 ConstraintsSoFar.size())
John Thompsoneac6e1d2010-09-13 18:15:37 +0000171 return true;
172 // Note that operand #n has a matching input.
173 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
Daniil Fukalov65f17352017-10-25 12:51:32 +0000174 assert(ConstraintsSoFar[N].MatchingInput >= 0);
John Thompsoneac6e1d2010-09-13 18:15:37 +0000175 }
176 } else if (*I == '|') {
177 multipleAlternativeIndex++;
178 pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
179 ++I;
Eric Christopher5fab03d2011-06-02 19:26:37 +0000180 } else if (*I == '^') {
181 // Multi-letter constraint
Eric Christopherec281c82011-06-03 22:09:12 +0000182 // FIXME: For now assuming these are 2-character constraints.
Alexander Potapenko1c9f2a92016-05-23 13:58:04 +0000183 pCodes->push_back(StringRef(I+1, 2));
Eric Christopherec281c82011-06-03 22:09:12 +0000184 I += 3;
Chris Lattnera55079a2006-02-01 01:29:47 +0000185 } else {
186 // Single letter constraint.
Alexander Potapenko1c9f2a92016-05-23 13:58:04 +0000187 pCodes->push_back(StringRef(I, 1));
Chris Lattnera55079a2006-02-01 01:29:47 +0000188 ++I;
189 }
190 }
191
192 return false;
193}
194
John Thompsoneac6e1d2010-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 Thompson44ab89e2010-10-29 17:29:13 +0000207InlineAsm::ConstraintInfoVector
Daniel Dunbar2928c832009-11-06 10:58:06 +0000208InlineAsm::ParseConstraints(StringRef Constraints) {
John Thompson44ab89e2010-10-29 17:29:13 +0000209 ConstraintInfoVector Result;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000210
Chris Lattner3b917782006-01-26 00:48:33 +0000211 // Scan the constraints string.
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000212 for (StringRef::iterator I = Constraints.begin(),
213 E = Constraints.end(); I != E; ) {
Chris Lattnera55079a2006-02-01 01:29:47 +0000214 ConstraintInfo Info;
215
216 // Find the end of this constraint.
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000217 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattnera55079a2006-02-01 01:29:47 +0000218
219 if (ConstraintEnd == I || // Empty constraint like ",,"
Benjamin Kramer82d5bae2010-07-25 23:18:32 +0000220 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
Chris Lattner2f0eec62006-02-02 00:23:53 +0000221 Result.clear(); // Erroneous constraint?
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000222 break;
223 }
Chris Lattnera55079a2006-02-01 01:29:47 +0000224
225 Result.push_back(Info);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000226
Chris Lattnera55079a2006-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 Lattnerf0b415f2006-01-26 02:21:59 +0000230 if (I != E) {
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000231 ++I;
Eric Christopherbeded8f2015-02-10 21:15:06 +0000232 if (I == E) {
233 Result.clear();
234 break;
235 } // don't allow "xyz,"
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000236 }
237 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000238
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000239 return Result;
240}
241
Chris Lattnerf0b415f2006-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 Lattnerdb125cf2011-07-18 04:54:35 +0000244bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000245 if (Ty->isVarArg()) return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000246
John Thompson44ab89e2010-10-29 17:29:13 +0000247 ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000248
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000249 // Error parsing constraints.
250 if (Constraints.empty() && !ConstStr.empty()) return false;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000251
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000252 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattnerea21ad42008-05-22 04:46:38 +0000253 unsigned NumIndirect = 0;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000254
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000255 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattnera55079a2006-02-01 01:29:47 +0000256 switch (Constraints[i].Type) {
257 case InlineAsm::isOutput:
Chris Lattnerea21ad42008-05-22 04:46:38 +0000258 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
259 return false; // outputs before inputs and clobbers.
Chris Lattner73d0d0d2007-04-28 01:02:58 +0000260 if (!Constraints[i].isIndirect) {
Chris Lattnera55079a2006-02-01 01:29:47 +0000261 ++NumOutputs;
262 break;
263 }
Chris Lattnerea21ad42008-05-22 04:46:38 +0000264 ++NumIndirect;
Justin Bogner6673ea82016-08-17 05:10:15 +0000265 LLVM_FALLTHROUGH; // We fall through for Indirect Outputs.
Chris Lattnera55079a2006-02-01 01:29:47 +0000266 case InlineAsm::isInput:
Chris Lattner3b917782006-01-26 00:48:33 +0000267 if (NumClobbers) return false; // inputs before clobbers.
268 ++NumInputs;
269 break;
Chris Lattnera55079a2006-02-01 01:29:47 +0000270 case InlineAsm::isClobber:
Chris Lattner3b917782006-01-26 00:48:33 +0000271 ++NumClobbers;
272 break;
273 }
Chris Lattner3b917782006-01-26 00:48:33 +0000274 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000275
Chris Lattner96bb6222008-04-27 23:33:55 +0000276 switch (NumOutputs) {
277 case 0:
Benjamin Kramerf0127052010-01-05 13:12:22 +0000278 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner96bb6222008-04-27 23:33:55 +0000279 break;
280 case 1:
Duncan Sands1df98592010-02-16 11:11:14 +0000281 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner96bb6222008-04-27 23:33:55 +0000282 break;
283 default:
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000284 StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
Craig Topperec0f0bc2014-04-09 06:08:46 +0000285 if (!STy || STy->getNumElements() != NumOutputs)
Chris Lattner96bb6222008-04-27 23:33:55 +0000286 return false;
287 break;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000288 }
289
Chris Lattner3b917782006-01-26 00:48:33 +0000290 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattner80cd1152006-01-25 22:26:05 +0000291 return true;
292}