blob: 99da7caaccf09ad1ee6e85271024449feb0bdc29 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnereef2fe72006-01-24 04:13:11 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the InlineAsm class.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "llvm/IR/InlineAsm.h"
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000014#include "ConstantsContext.h"
15#include "LLVMContextImpl.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000016#include "llvm/ADT/StringRef.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000018#include "llvm/IR/LLVMContext.h"
19#include "llvm/IR/Value.h"
20#include "llvm/Support/Casting.h"
21#include "llvm/Support/Compiler.h"
Jeff Cohenb24b66f2006-02-01 04:37:04 +000022#include <algorithm>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000023#include <cassert>
Chris Lattner8547e3a2006-01-26 00:48:33 +000024#include <cctype>
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000025#include <cstddef>
26#include <cstdlib>
27
Chris Lattnereef2fe72006-01-24 04:13:11 +000028using namespace llvm;
29
David Blaikie71c9c9c2015-07-28 00:06:38 +000030InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000031 const std::string &constraints, bool hasSideEffects,
Chad Rosierd8c76102012-09-05 19:00:49 +000032 bool isAlignStack, AsmDialect asmDialect)
David Blaikie71c9c9c2015-07-28 00:06:38 +000033 : Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),
34 AsmString(asmString), Constraints(constraints), FTy(FTy),
35 HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
36 Dialect(asmDialect) {
Chris Lattnera2d810d2006-01-25 22:26:05 +000037 // Do various checks on the constraint string and type.
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000038 assert(Verify(getFunctionType(), constraints) &&
39 "Function type not legal for constraints!");
40}
41
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000042InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,
43 StringRef Constraints, bool hasSideEffects,
44 bool isAlignStack, AsmDialect asmDialect) {
45 InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,
46 isAlignStack, asmDialect);
47 LLVMContextImpl *pImpl = FTy->getContext().pImpl;
48 return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);
49}
50
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000051void InlineAsm::destroyConstant() {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000052 getType()->getContext().pImpl->InlineAsms.remove(this);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000053 delete this;
Chris Lattnereef2fe72006-01-24 04:13:11 +000054}
55
Chris Lattner1d021a92011-07-15 23:15:45 +000056FunctionType *InlineAsm::getFunctionType() const {
David Blaikie71c9c9c2015-07-28 00:06:38 +000057 return FTy;
Chris Lattnereef2fe72006-01-24 04:13:11 +000058}
Fangrui Songf78650a2018-07-30 19:41:25 +000059
Chris Lattner7ed31012006-02-01 01:29:47 +000060/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
61/// fields in this structure. If the constraint string is not understood,
62/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000063bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
John Thompsone8360b72010-10-29 17:29:13 +000064 InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000065 StringRef::iterator I = Str.begin(), E = Str.end();
John Thompson1094c802010-09-13 18:15:37 +000066 unsigned multipleAlternativeCount = Str.count('|') + 1;
67 unsigned multipleAlternativeIndex = 0;
John Thompsone8360b72010-10-29 17:29:13 +000068 ConstraintCodeVector *pCodes = &Codes;
Eric Christopherf3e79e82015-02-10 21:15:06 +000069
Chris Lattner7ed31012006-02-01 01:29:47 +000070 // Initialize
David Blaikiedc3f01e2015-03-09 01:57:13 +000071 isMultipleAlternative = multipleAlternativeCount > 1;
John Thompson1094c802010-09-13 18:15:37 +000072 if (isMultipleAlternative) {
73 multipleAlternatives.resize(multipleAlternativeCount);
74 pCodes = &multipleAlternatives[0].Codes;
75 }
Chris Lattner7ed31012006-02-01 01:29:47 +000076 Type = isInput;
77 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000078 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000079 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000080 isIndirect = false;
John Thompson1094c802010-09-13 18:15:37 +000081 currentAlternativeIndex = 0;
Fangrui Songf78650a2018-07-30 19:41:25 +000082
Chris Lattnerc48e2c32007-04-28 01:02:58 +000083 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000084 if (*I == '~') {
85 Type = isClobber;
86 ++I;
Akira Hatanaka489dece2014-09-05 22:30:32 +000087
88 // '{' must immediately follow '~'.
89 if (I != E && *I != '{')
90 return true;
Chris Lattner7ed31012006-02-01 01:29:47 +000091 } else if (*I == '=') {
92 ++I;
93 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000094 }
Eric Christopherf3e79e82015-02-10 21:15:06 +000095
Chris Lattnerc48e2c32007-04-28 01:02:58 +000096 if (*I == '*') {
97 isIndirect = true;
98 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +000099 }
Eric Christopherf3e79e82015-02-10 21:15:06 +0000100
Chris Lattner7ed31012006-02-01 01:29:47 +0000101 if (I == E) return true; // Just a prefix, like "==" or "~".
Fangrui Songf78650a2018-07-30 19:41:25 +0000102
Chris Lattner7ed31012006-02-01 01:29:47 +0000103 // Parse the modifiers.
104 bool DoneWithModifiers = false;
105 while (!DoneWithModifiers) {
106 switch (*I) {
107 default:
108 DoneWithModifiers = true;
109 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000110 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +0000111 if (Type != isOutput || // Cannot early clobber anything but output.
112 isEarlyClobber) // Reject &&&&&&
113 return true;
114 isEarlyClobber = true;
115 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000116 case '%': // Commutative.
117 if (Type == isClobber || // Cannot commute clobbers.
118 isCommutative) // Reject %%%%%
119 return true;
120 isCommutative = true;
121 break;
122 case '#': // Comment.
123 case '*': // Register preferencing.
124 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000125 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000126
Chris Lattner7ed31012006-02-01 01:29:47 +0000127 if (!DoneWithModifiers) {
128 ++I;
129 if (I == E) return true; // Just prefixes and modifiers!
130 }
131 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000132
Chris Lattner7ed31012006-02-01 01:29:47 +0000133 // Parse the various constraints.
134 while (I != E) {
135 if (*I == '{') { // Physical register reference.
136 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000137 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000138 if (ConstraintEnd == E) return true; // "{foo"
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000139 pCodes->push_back(StringRef(I, ConstraintEnd+1 - I));
Chris Lattner7ed31012006-02-01 01:29:47 +0000140 I = ConstraintEnd+1;
Guy Benyei83c74e92013-02-12 21:21:59 +0000141 } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000142 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000143 StringRef::iterator NumStart = I;
Guy Benyei83c74e92013-02-12 21:21:59 +0000144 while (I != E && isdigit(static_cast<unsigned char>(*I)))
Chris Lattner7ed31012006-02-01 01:29:47 +0000145 ++I;
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000146 pCodes->push_back(StringRef(NumStart, I - NumStart));
John Thompson1094c802010-09-13 18:15:37 +0000147 unsigned N = atoi(pCodes->back().c_str());
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000148 // Check that this is a valid matching constraint!
149 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
150 Type != isInput)
151 return true; // Invalid constraint number.
Fangrui Songf78650a2018-07-30 19:41:25 +0000152
Chris Lattner860df6e2008-10-17 16:47:46 +0000153 // If Operand N already has a matching input, reject this. An output
154 // can't be constrained to the same value as multiple inputs.
John Thompson1094c802010-09-13 18:15:37 +0000155 if (isMultipleAlternative) {
Karl Schimpff04a5d52015-09-03 15:41:37 +0000156 if (multipleAlternativeIndex >=
157 ConstraintsSoFar[N].multipleAlternatives.size())
Karl Schimpf388fd5a2015-09-03 15:41:34 +0000158 return true;
John Thompson1094c802010-09-13 18:15:37 +0000159 InlineAsm::SubConstraintInfo &scInfo =
160 ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
161 if (scInfo.MatchingInput != -1)
162 return true;
163 // Note that operand #n has a matching input.
164 scInfo.MatchingInput = ConstraintsSoFar.size();
Daniil Fukalov2bfbadc2017-10-25 12:51:32 +0000165 assert(scInfo.MatchingInput >= 0);
John Thompson1094c802010-09-13 18:15:37 +0000166 } else {
Benjamin Kramer9de151e2015-03-29 20:33:07 +0000167 if (ConstraintsSoFar[N].hasMatchingInput() &&
Benjamin Kramer27395712015-03-29 20:49:03 +0000168 (size_t)ConstraintsSoFar[N].MatchingInput !=
169 ConstraintsSoFar.size())
John Thompson1094c802010-09-13 18:15:37 +0000170 return true;
171 // Note that operand #n has a matching input.
172 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
Daniil Fukalov2bfbadc2017-10-25 12:51:32 +0000173 assert(ConstraintsSoFar[N].MatchingInput >= 0);
John Thompson1094c802010-09-13 18:15:37 +0000174 }
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.
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000182 pCodes->push_back(StringRef(I+1, 2));
Eric Christopher1e3e8932011-06-03 22:09:12 +0000183 I += 3;
Chris Lattner7ed31012006-02-01 01:29:47 +0000184 } else {
185 // Single letter constraint.
Alexander Potapenko7d68c262016-05-23 13:58:04 +0000186 pCodes->push_back(StringRef(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;
Fangrui Songf78650a2018-07-30 19:41:25 +0000209
Chris Lattner8547e3a2006-01-26 00:48:33 +0000210 // 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);
Fangrui Songf78650a2018-07-30 19:41:25 +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;
Eric Christopherf3e79e82015-02-10 21:15:06 +0000231 if (I == E) {
232 Result.clear();
233 break;
234 } // don't allow "xyz,"
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000235 }
236 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000237
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000238 return Result;
239}
240
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000241/// Verify - Verify that the specified constraint string is reasonable for the
242/// specified function type, and otherwise validate the constraint string.
Chris Lattner229907c2011-07-18 04:54:35 +0000243bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000244 if (Ty->isVarArg()) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +0000245
John Thompsone8360b72010-10-29 17:29:13 +0000246 ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
Fangrui Songf78650a2018-07-30 19:41:25 +0000247
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000248 // Error parsing constraints.
249 if (Constraints.empty() && !ConstStr.empty()) return false;
Fangrui Songf78650a2018-07-30 19:41:25 +0000250
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000251 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000252 unsigned NumIndirect = 0;
Fangrui Songf78650a2018-07-30 19:41:25 +0000253
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000254 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000255 switch (Constraints[i].Type) {
256 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000257 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
258 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000259 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000260 ++NumOutputs;
261 break;
262 }
Chris Lattneree00d042008-05-22 04:46:38 +0000263 ++NumIndirect;
Justin Bognerb03fd122016-08-17 05:10:15 +0000264 LLVM_FALLTHROUGH; // We fall through for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000265 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000266 if (NumClobbers) return false; // inputs before clobbers.
267 ++NumInputs;
268 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000269 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000270 ++NumClobbers;
271 break;
272 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000273 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000274
Chris Lattner10748d82008-04-27 23:33:55 +0000275 switch (NumOutputs) {
276 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000277 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000278 break;
279 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000280 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000281 break;
282 default:
Chris Lattner229907c2011-07-18 04:54:35 +0000283 StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
Craig Topperc6207612014-04-09 06:08:46 +0000284 if (!STy || STy->getNumElements() != NumOutputs)
Chris Lattner10748d82008-04-27 23:33:55 +0000285 return false;
286 break;
Fangrui Songf78650a2018-07-30 19:41:25 +0000287 }
288
Chris Lattner8547e3a2006-01-26 00:48:33 +0000289 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000290 return true;
291}