annotate llvm/lib/IR/InlineAsm.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements the InlineAsm class.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "llvm/IR/InlineAsm.h"
anatofuz
parents:
diff changeset
14 #include "ConstantsContext.h"
anatofuz
parents:
diff changeset
15 #include "LLVMContextImpl.h"
anatofuz
parents:
diff changeset
16 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
17 #include "llvm/IR/DerivedTypes.h"
anatofuz
parents:
diff changeset
18 #include "llvm/IR/LLVMContext.h"
anatofuz
parents:
diff changeset
19 #include "llvm/IR/Value.h"
anatofuz
parents:
diff changeset
20 #include "llvm/Support/Casting.h"
anatofuz
parents:
diff changeset
21 #include "llvm/Support/Compiler.h"
anatofuz
parents:
diff changeset
22 #include <algorithm>
anatofuz
parents:
diff changeset
23 #include <cassert>
anatofuz
parents:
diff changeset
24 #include <cctype>
anatofuz
parents:
diff changeset
25 #include <cstddef>
anatofuz
parents:
diff changeset
26 #include <cstdlib>
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 using namespace llvm;
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
anatofuz
parents:
diff changeset
31 const std::string &constraints, bool hasSideEffects,
anatofuz
parents:
diff changeset
32 bool isAlignStack, AsmDialect asmDialect)
anatofuz
parents:
diff changeset
33 : Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),
anatofuz
parents:
diff changeset
34 AsmString(asmString), Constraints(constraints), FTy(FTy),
anatofuz
parents:
diff changeset
35 HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
anatofuz
parents:
diff changeset
36 Dialect(asmDialect) {
anatofuz
parents:
diff changeset
37 // Do various checks on the constraint string and type.
anatofuz
parents:
diff changeset
38 assert(Verify(getFunctionType(), constraints) &&
anatofuz
parents:
diff changeset
39 "Function type not legal for constraints!");
anatofuz
parents:
diff changeset
40 }
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,
anatofuz
parents:
diff changeset
43 StringRef Constraints, bool hasSideEffects,
anatofuz
parents:
diff changeset
44 bool isAlignStack, AsmDialect asmDialect) {
anatofuz
parents:
diff changeset
45 InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,
anatofuz
parents:
diff changeset
46 isAlignStack, asmDialect);
anatofuz
parents:
diff changeset
47 LLVMContextImpl *pImpl = FTy->getContext().pImpl;
anatofuz
parents:
diff changeset
48 return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);
anatofuz
parents:
diff changeset
49 }
anatofuz
parents:
diff changeset
50
anatofuz
parents:
diff changeset
51 void InlineAsm::destroyConstant() {
anatofuz
parents:
diff changeset
52 getType()->getContext().pImpl->InlineAsms.remove(this);
anatofuz
parents:
diff changeset
53 delete this;
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 FunctionType *InlineAsm::getFunctionType() const {
anatofuz
parents:
diff changeset
57 return FTy;
anatofuz
parents:
diff changeset
58 }
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
anatofuz
parents:
diff changeset
61 /// fields in this structure. If the constraint string is not understood,
anatofuz
parents:
diff changeset
62 /// return true, otherwise return false.
anatofuz
parents:
diff changeset
63 bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
anatofuz
parents:
diff changeset
64 InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
anatofuz
parents:
diff changeset
65 StringRef::iterator I = Str.begin(), E = Str.end();
anatofuz
parents:
diff changeset
66 unsigned multipleAlternativeCount = Str.count('|') + 1;
anatofuz
parents:
diff changeset
67 unsigned multipleAlternativeIndex = 0;
anatofuz
parents:
diff changeset
68 ConstraintCodeVector *pCodes = &Codes;
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 // Initialize
anatofuz
parents:
diff changeset
71 isMultipleAlternative = multipleAlternativeCount > 1;
anatofuz
parents:
diff changeset
72 if (isMultipleAlternative) {
anatofuz
parents:
diff changeset
73 multipleAlternatives.resize(multipleAlternativeCount);
anatofuz
parents:
diff changeset
74 pCodes = &multipleAlternatives[0].Codes;
anatofuz
parents:
diff changeset
75 }
anatofuz
parents:
diff changeset
76 Type = isInput;
anatofuz
parents:
diff changeset
77 isEarlyClobber = false;
anatofuz
parents:
diff changeset
78 MatchingInput = -1;
anatofuz
parents:
diff changeset
79 isCommutative = false;
anatofuz
parents:
diff changeset
80 isIndirect = false;
anatofuz
parents:
diff changeset
81 currentAlternativeIndex = 0;
anatofuz
parents:
diff changeset
82
anatofuz
parents:
diff changeset
83 // Parse prefixes.
anatofuz
parents:
diff changeset
84 if (*I == '~') {
anatofuz
parents:
diff changeset
85 Type = isClobber;
anatofuz
parents:
diff changeset
86 ++I;
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 // '{' must immediately follow '~'.
anatofuz
parents:
diff changeset
89 if (I != E && *I != '{')
anatofuz
parents:
diff changeset
90 return true;
anatofuz
parents:
diff changeset
91 } else if (*I == '=') {
anatofuz
parents:
diff changeset
92 ++I;
anatofuz
parents:
diff changeset
93 Type = isOutput;
anatofuz
parents:
diff changeset
94 }
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 if (*I == '*') {
anatofuz
parents:
diff changeset
97 isIndirect = true;
anatofuz
parents:
diff changeset
98 ++I;
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 if (I == E) return true; // Just a prefix, like "==" or "~".
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 // Parse the modifiers.
anatofuz
parents:
diff changeset
104 bool DoneWithModifiers = false;
anatofuz
parents:
diff changeset
105 while (!DoneWithModifiers) {
anatofuz
parents:
diff changeset
106 switch (*I) {
anatofuz
parents:
diff changeset
107 default:
anatofuz
parents:
diff changeset
108 DoneWithModifiers = true;
anatofuz
parents:
diff changeset
109 break;
anatofuz
parents:
diff changeset
110 case '&': // Early clobber.
anatofuz
parents:
diff changeset
111 if (Type != isOutput || // Cannot early clobber anything but output.
anatofuz
parents:
diff changeset
112 isEarlyClobber) // Reject &&&&&&
anatofuz
parents:
diff changeset
113 return true;
anatofuz
parents:
diff changeset
114 isEarlyClobber = true;
anatofuz
parents:
diff changeset
115 break;
anatofuz
parents:
diff changeset
116 case '%': // Commutative.
anatofuz
parents:
diff changeset
117 if (Type == isClobber || // Cannot commute clobbers.
anatofuz
parents:
diff changeset
118 isCommutative) // Reject %%%%%
anatofuz
parents:
diff changeset
119 return true;
anatofuz
parents:
diff changeset
120 isCommutative = true;
anatofuz
parents:
diff changeset
121 break;
anatofuz
parents:
diff changeset
122 case '#': // Comment.
anatofuz
parents:
diff changeset
123 case '*': // Register preferencing.
anatofuz
parents:
diff changeset
124 return true; // Not supported.
anatofuz
parents:
diff changeset
125 }
anatofuz
parents:
diff changeset
126
anatofuz
parents:
diff changeset
127 if (!DoneWithModifiers) {
anatofuz
parents:
diff changeset
128 ++I;
anatofuz
parents:
diff changeset
129 if (I == E) return true; // Just prefixes and modifiers!
anatofuz
parents:
diff changeset
130 }
anatofuz
parents:
diff changeset
131 }
anatofuz
parents:
diff changeset
132
anatofuz
parents:
diff changeset
133 // Parse the various constraints.
anatofuz
parents:
diff changeset
134 while (I != E) {
anatofuz
parents:
diff changeset
135 if (*I == '{') { // Physical register reference.
anatofuz
parents:
diff changeset
136 // Find the end of the register name.
anatofuz
parents:
diff changeset
137 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
anatofuz
parents:
diff changeset
138 if (ConstraintEnd == E) return true; // "{foo"
anatofuz
parents:
diff changeset
139 pCodes->push_back(std::string(StringRef(I, ConstraintEnd + 1 - I)));
anatofuz
parents:
diff changeset
140 I = ConstraintEnd+1;
anatofuz
parents:
diff changeset
141 } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
anatofuz
parents:
diff changeset
142 // Maximal munch numbers.
anatofuz
parents:
diff changeset
143 StringRef::iterator NumStart = I;
anatofuz
parents:
diff changeset
144 while (I != E && isdigit(static_cast<unsigned char>(*I)))
anatofuz
parents:
diff changeset
145 ++I;
anatofuz
parents:
diff changeset
146 pCodes->push_back(std::string(StringRef(NumStart, I - NumStart)));
anatofuz
parents:
diff changeset
147 unsigned N = atoi(pCodes->back().c_str());
anatofuz
parents:
diff changeset
148 // Check that this is a valid matching constraint!
anatofuz
parents:
diff changeset
149 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
anatofuz
parents:
diff changeset
150 Type != isInput)
anatofuz
parents:
diff changeset
151 return true; // Invalid constraint number.
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 // If Operand N already has a matching input, reject this. An output
anatofuz
parents:
diff changeset
154 // can't be constrained to the same value as multiple inputs.
anatofuz
parents:
diff changeset
155 if (isMultipleAlternative) {
anatofuz
parents:
diff changeset
156 if (multipleAlternativeIndex >=
anatofuz
parents:
diff changeset
157 ConstraintsSoFar[N].multipleAlternatives.size())
anatofuz
parents:
diff changeset
158 return true;
anatofuz
parents:
diff changeset
159 InlineAsm::SubConstraintInfo &scInfo =
anatofuz
parents:
diff changeset
160 ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
anatofuz
parents:
diff changeset
161 if (scInfo.MatchingInput != -1)
anatofuz
parents:
diff changeset
162 return true;
anatofuz
parents:
diff changeset
163 // Note that operand #n has a matching input.
anatofuz
parents:
diff changeset
164 scInfo.MatchingInput = ConstraintsSoFar.size();
anatofuz
parents:
diff changeset
165 assert(scInfo.MatchingInput >= 0);
anatofuz
parents:
diff changeset
166 } else {
anatofuz
parents:
diff changeset
167 if (ConstraintsSoFar[N].hasMatchingInput() &&
anatofuz
parents:
diff changeset
168 (size_t)ConstraintsSoFar[N].MatchingInput !=
anatofuz
parents:
diff changeset
169 ConstraintsSoFar.size())
anatofuz
parents:
diff changeset
170 return true;
anatofuz
parents:
diff changeset
171 // Note that operand #n has a matching input.
anatofuz
parents:
diff changeset
172 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
anatofuz
parents:
diff changeset
173 assert(ConstraintsSoFar[N].MatchingInput >= 0);
anatofuz
parents:
diff changeset
174 }
anatofuz
parents:
diff changeset
175 } else if (*I == '|') {
anatofuz
parents:
diff changeset
176 multipleAlternativeIndex++;
anatofuz
parents:
diff changeset
177 pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
anatofuz
parents:
diff changeset
178 ++I;
anatofuz
parents:
diff changeset
179 } else if (*I == '^') {
anatofuz
parents:
diff changeset
180 // Multi-letter constraint
anatofuz
parents:
diff changeset
181 // FIXME: For now assuming these are 2-character constraints.
anatofuz
parents:
diff changeset
182 pCodes->push_back(std::string(StringRef(I + 1, 2)));
anatofuz
parents:
diff changeset
183 I += 3;
anatofuz
parents:
diff changeset
184 } else if (*I == '@') {
anatofuz
parents:
diff changeset
185 // Multi-letter constraint
anatofuz
parents:
diff changeset
186 ++I;
anatofuz
parents:
diff changeset
187 unsigned char C = static_cast<unsigned char>(*I);
anatofuz
parents:
diff changeset
188 assert(isdigit(C) && "Expected a digit!");
anatofuz
parents:
diff changeset
189 int N = C - '0';
anatofuz
parents:
diff changeset
190 assert(N > 0 && "Found a zero letter constraint!");
anatofuz
parents:
diff changeset
191 ++I;
anatofuz
parents:
diff changeset
192 pCodes->push_back(std::string(StringRef(I, N)));
anatofuz
parents:
diff changeset
193 I += N;
anatofuz
parents:
diff changeset
194 } else {
anatofuz
parents:
diff changeset
195 // Single letter constraint.
anatofuz
parents:
diff changeset
196 pCodes->push_back(std::string(StringRef(I, 1)));
anatofuz
parents:
diff changeset
197 ++I;
anatofuz
parents:
diff changeset
198 }
anatofuz
parents:
diff changeset
199 }
anatofuz
parents:
diff changeset
200
anatofuz
parents:
diff changeset
201 return false;
anatofuz
parents:
diff changeset
202 }
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 /// selectAlternative - Point this constraint to the alternative constraint
anatofuz
parents:
diff changeset
205 /// indicated by the index.
anatofuz
parents:
diff changeset
206 void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
anatofuz
parents:
diff changeset
207 if (index < multipleAlternatives.size()) {
anatofuz
parents:
diff changeset
208 currentAlternativeIndex = index;
anatofuz
parents:
diff changeset
209 InlineAsm::SubConstraintInfo &scInfo =
anatofuz
parents:
diff changeset
210 multipleAlternatives[currentAlternativeIndex];
anatofuz
parents:
diff changeset
211 MatchingInput = scInfo.MatchingInput;
anatofuz
parents:
diff changeset
212 Codes = scInfo.Codes;
anatofuz
parents:
diff changeset
213 }
anatofuz
parents:
diff changeset
214 }
anatofuz
parents:
diff changeset
215
anatofuz
parents:
diff changeset
216 InlineAsm::ConstraintInfoVector
anatofuz
parents:
diff changeset
217 InlineAsm::ParseConstraints(StringRef Constraints) {
anatofuz
parents:
diff changeset
218 ConstraintInfoVector Result;
anatofuz
parents:
diff changeset
219
anatofuz
parents:
diff changeset
220 // Scan the constraints string.
anatofuz
parents:
diff changeset
221 for (StringRef::iterator I = Constraints.begin(),
anatofuz
parents:
diff changeset
222 E = Constraints.end(); I != E; ) {
anatofuz
parents:
diff changeset
223 ConstraintInfo Info;
anatofuz
parents:
diff changeset
224
anatofuz
parents:
diff changeset
225 // Find the end of this constraint.
anatofuz
parents:
diff changeset
226 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
anatofuz
parents:
diff changeset
227
anatofuz
parents:
diff changeset
228 if (ConstraintEnd == I || // Empty constraint like ",,"
anatofuz
parents:
diff changeset
229 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
anatofuz
parents:
diff changeset
230 Result.clear(); // Erroneous constraint?
anatofuz
parents:
diff changeset
231 break;
anatofuz
parents:
diff changeset
232 }
anatofuz
parents:
diff changeset
233
anatofuz
parents:
diff changeset
234 Result.push_back(Info);
anatofuz
parents:
diff changeset
235
anatofuz
parents:
diff changeset
236 // ConstraintEnd may be either the next comma or the end of the string. In
anatofuz
parents:
diff changeset
237 // the former case, we skip the comma.
anatofuz
parents:
diff changeset
238 I = ConstraintEnd;
anatofuz
parents:
diff changeset
239 if (I != E) {
anatofuz
parents:
diff changeset
240 ++I;
anatofuz
parents:
diff changeset
241 if (I == E) {
anatofuz
parents:
diff changeset
242 Result.clear();
anatofuz
parents:
diff changeset
243 break;
anatofuz
parents:
diff changeset
244 } // don't allow "xyz,"
anatofuz
parents:
diff changeset
245 }
anatofuz
parents:
diff changeset
246 }
anatofuz
parents:
diff changeset
247
anatofuz
parents:
diff changeset
248 return Result;
anatofuz
parents:
diff changeset
249 }
anatofuz
parents:
diff changeset
250
anatofuz
parents:
diff changeset
251 /// Verify - Verify that the specified constraint string is reasonable for the
anatofuz
parents:
diff changeset
252 /// specified function type, and otherwise validate the constraint string.
anatofuz
parents:
diff changeset
253 bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
anatofuz
parents:
diff changeset
254 if (Ty->isVarArg()) return false;
anatofuz
parents:
diff changeset
255
anatofuz
parents:
diff changeset
256 ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
anatofuz
parents:
diff changeset
257
anatofuz
parents:
diff changeset
258 // Error parsing constraints.
anatofuz
parents:
diff changeset
259 if (Constraints.empty() && !ConstStr.empty()) return false;
anatofuz
parents:
diff changeset
260
anatofuz
parents:
diff changeset
261 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
anatofuz
parents:
diff changeset
262 unsigned NumIndirect = 0;
anatofuz
parents:
diff changeset
263
anatofuz
parents:
diff changeset
264 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
anatofuz
parents:
diff changeset
265 switch (Constraints[i].Type) {
anatofuz
parents:
diff changeset
266 case InlineAsm::isOutput:
anatofuz
parents:
diff changeset
267 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
anatofuz
parents:
diff changeset
268 return false; // outputs before inputs and clobbers.
anatofuz
parents:
diff changeset
269 if (!Constraints[i].isIndirect) {
anatofuz
parents:
diff changeset
270 ++NumOutputs;
anatofuz
parents:
diff changeset
271 break;
anatofuz
parents:
diff changeset
272 }
anatofuz
parents:
diff changeset
273 ++NumIndirect;
anatofuz
parents:
diff changeset
274 LLVM_FALLTHROUGH; // We fall through for Indirect Outputs.
anatofuz
parents:
diff changeset
275 case InlineAsm::isInput:
anatofuz
parents:
diff changeset
276 if (NumClobbers) return false; // inputs before clobbers.
anatofuz
parents:
diff changeset
277 ++NumInputs;
anatofuz
parents:
diff changeset
278 break;
anatofuz
parents:
diff changeset
279 case InlineAsm::isClobber:
anatofuz
parents:
diff changeset
280 ++NumClobbers;
anatofuz
parents:
diff changeset
281 break;
anatofuz
parents:
diff changeset
282 }
anatofuz
parents:
diff changeset
283 }
anatofuz
parents:
diff changeset
284
anatofuz
parents:
diff changeset
285 switch (NumOutputs) {
anatofuz
parents:
diff changeset
286 case 0:
anatofuz
parents:
diff changeset
287 if (!Ty->getReturnType()->isVoidTy()) return false;
anatofuz
parents:
diff changeset
288 break;
anatofuz
parents:
diff changeset
289 case 1:
anatofuz
parents:
diff changeset
290 if (Ty->getReturnType()->isStructTy()) return false;
anatofuz
parents:
diff changeset
291 break;
anatofuz
parents:
diff changeset
292 default:
anatofuz
parents:
diff changeset
293 StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
anatofuz
parents:
diff changeset
294 if (!STy || STy->getNumElements() != NumOutputs)
anatofuz
parents:
diff changeset
295 return false;
anatofuz
parents:
diff changeset
296 break;
anatofuz
parents:
diff changeset
297 }
anatofuz
parents:
diff changeset
298
anatofuz
parents:
diff changeset
299 if (Ty->getNumParams() != NumInputs) return false;
anatofuz
parents:
diff changeset
300 return true;
anatofuz
parents:
diff changeset
301 }