1 //===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file implements the InlineAsm class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/InlineAsm.h"
14 #include "ConstantsContext.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Value.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Errc.h"
30 InlineAsm::InlineAsm(FunctionType
*FTy
, const std::string
&asmString
,
31 const std::string
&constraints
, bool hasSideEffects
,
32 bool isAlignStack
, AsmDialect asmDialect
, bool canThrow
)
33 : Value(PointerType::getUnqual(FTy
), Value::InlineAsmVal
),
34 AsmString(asmString
), Constraints(constraints
), FTy(FTy
),
35 HasSideEffects(hasSideEffects
), IsAlignStack(isAlignStack
),
36 Dialect(asmDialect
), CanThrow(canThrow
) {
38 // Do various checks on the constraint string and type.
39 cantFail(verify(getFunctionType(), constraints
));
43 InlineAsm
*InlineAsm::get(FunctionType
*FTy
, StringRef AsmString
,
44 StringRef Constraints
, bool hasSideEffects
,
45 bool isAlignStack
, AsmDialect asmDialect
,
47 InlineAsmKeyType
Key(AsmString
, Constraints
, FTy
, hasSideEffects
,
48 isAlignStack
, asmDialect
, canThrow
);
49 LLVMContextImpl
*pImpl
= FTy
->getContext().pImpl
;
50 return pImpl
->InlineAsms
.getOrCreate(PointerType::getUnqual(FTy
), Key
);
53 void InlineAsm::destroyConstant() {
54 getType()->getContext().pImpl
->InlineAsms
.remove(this);
58 FunctionType
*InlineAsm::getFunctionType() const {
62 void InlineAsm::collectAsmStrs(SmallVectorImpl
<StringRef
> &AsmStrs
) const {
63 StringRef
AsmStr(AsmString
);
66 // TODO: 1) Unify delimiter for inline asm, we also meet other delimiters
67 // for example "\0A", ";".
68 // 2) Enhance StringRef. Some of the special delimiter ("\0") can't be
69 // split in StringRef. Also empty StringRef can not call split (will stuck).
72 AsmStr
.split(AsmStrs
, "\n\t", -1, false);
75 /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
76 /// fields in this structure. If the constraint string is not understood,
77 /// return true, otherwise return false.
78 bool InlineAsm::ConstraintInfo::Parse(StringRef Str
,
79 InlineAsm::ConstraintInfoVector
&ConstraintsSoFar
) {
80 StringRef::iterator I
= Str
.begin(), E
= Str
.end();
81 unsigned multipleAlternativeCount
= Str
.count('|') + 1;
82 unsigned multipleAlternativeIndex
= 0;
83 ConstraintCodeVector
*pCodes
= &Codes
;
86 isMultipleAlternative
= multipleAlternativeCount
> 1;
87 if (isMultipleAlternative
) {
88 multipleAlternatives
.resize(multipleAlternativeCount
);
89 pCodes
= &multipleAlternatives
[0].Codes
;
92 isEarlyClobber
= false;
94 isCommutative
= false;
96 currentAlternativeIndex
= 0;
103 // '{' must immediately follow '~'.
104 if (I
!= E
&& *I
!= '{')
106 } else if (*I
== '=') {
109 } else if (*I
== '!') {
119 if (I
== E
) return true; // Just a prefix, like "==" or "~".
121 // Parse the modifiers.
122 bool DoneWithModifiers
= false;
123 while (!DoneWithModifiers
) {
126 DoneWithModifiers
= true;
128 case '&': // Early clobber.
129 if (Type
!= isOutput
|| // Cannot early clobber anything but output.
130 isEarlyClobber
) // Reject &&&&&&
132 isEarlyClobber
= true;
134 case '%': // Commutative.
135 if (Type
== isClobber
|| // Cannot commute clobbers.
136 isCommutative
) // Reject %%%%%
138 isCommutative
= true;
140 case '#': // Comment.
141 case '*': // Register preferencing.
142 return true; // Not supported.
145 if (!DoneWithModifiers
) {
147 if (I
== E
) return true; // Just prefixes and modifiers!
151 // Parse the various constraints.
153 if (*I
== '{') { // Physical register reference.
154 // Find the end of the register name.
155 StringRef::iterator ConstraintEnd
= std::find(I
+1, E
, '}');
156 if (ConstraintEnd
== E
) return true; // "{foo"
157 pCodes
->push_back(std::string(StringRef(I
, ConstraintEnd
+ 1 - I
)));
159 } else if (isdigit(static_cast<unsigned char>(*I
))) { // Matching Constraint
160 // Maximal munch numbers.
161 StringRef::iterator NumStart
= I
;
162 while (I
!= E
&& isdigit(static_cast<unsigned char>(*I
)))
164 pCodes
->push_back(std::string(StringRef(NumStart
, I
- NumStart
)));
165 unsigned N
= atoi(pCodes
->back().c_str());
166 // Check that this is a valid matching constraint!
167 if (N
>= ConstraintsSoFar
.size() || ConstraintsSoFar
[N
].Type
!= isOutput
||
169 return true; // Invalid constraint number.
171 // If Operand N already has a matching input, reject this. An output
172 // can't be constrained to the same value as multiple inputs.
173 if (isMultipleAlternative
) {
174 if (multipleAlternativeIndex
>=
175 ConstraintsSoFar
[N
].multipleAlternatives
.size())
177 InlineAsm::SubConstraintInfo
&scInfo
=
178 ConstraintsSoFar
[N
].multipleAlternatives
[multipleAlternativeIndex
];
179 if (scInfo
.MatchingInput
!= -1)
181 // Note that operand #n has a matching input.
182 scInfo
.MatchingInput
= ConstraintsSoFar
.size();
183 assert(scInfo
.MatchingInput
>= 0);
185 if (ConstraintsSoFar
[N
].hasMatchingInput() &&
186 (size_t)ConstraintsSoFar
[N
].MatchingInput
!=
187 ConstraintsSoFar
.size())
189 // Note that operand #n has a matching input.
190 ConstraintsSoFar
[N
].MatchingInput
= ConstraintsSoFar
.size();
191 assert(ConstraintsSoFar
[N
].MatchingInput
>= 0);
193 } else if (*I
== '|') {
194 multipleAlternativeIndex
++;
195 pCodes
= &multipleAlternatives
[multipleAlternativeIndex
].Codes
;
197 } else if (*I
== '^') {
198 // Multi-letter constraint
199 // FIXME: For now assuming these are 2-character constraints.
200 pCodes
->push_back(std::string(StringRef(I
+ 1, 2)));
202 } else if (*I
== '@') {
203 // Multi-letter constraint
205 unsigned char C
= static_cast<unsigned char>(*I
);
206 assert(isdigit(C
) && "Expected a digit!");
208 assert(N
> 0 && "Found a zero letter constraint!");
210 pCodes
->push_back(std::string(StringRef(I
, N
)));
213 // Single letter constraint.
214 pCodes
->push_back(std::string(StringRef(I
, 1)));
222 /// selectAlternative - Point this constraint to the alternative constraint
223 /// indicated by the index.
224 void InlineAsm::ConstraintInfo::selectAlternative(unsigned index
) {
225 if (index
< multipleAlternatives
.size()) {
226 currentAlternativeIndex
= index
;
227 InlineAsm::SubConstraintInfo
&scInfo
=
228 multipleAlternatives
[currentAlternativeIndex
];
229 MatchingInput
= scInfo
.MatchingInput
;
230 Codes
= scInfo
.Codes
;
234 InlineAsm::ConstraintInfoVector
235 InlineAsm::ParseConstraints(StringRef Constraints
) {
236 ConstraintInfoVector Result
;
238 // Scan the constraints string.
239 for (StringRef::iterator I
= Constraints
.begin(),
240 E
= Constraints
.end(); I
!= E
; ) {
243 // Find the end of this constraint.
244 StringRef::iterator ConstraintEnd
= std::find(I
, E
, ',');
246 if (ConstraintEnd
== I
|| // Empty constraint like ",,"
247 Info
.Parse(StringRef(I
, ConstraintEnd
-I
), Result
)) {
248 Result
.clear(); // Erroneous constraint?
252 Result
.push_back(Info
);
254 // ConstraintEnd may be either the next comma or the end of the string. In
255 // the former case, we skip the comma.
262 } // don't allow "xyz,"
269 static Error
makeStringError(const char *Msg
) {
270 return createStringError(errc::invalid_argument
, Msg
);
273 Error
InlineAsm::verify(FunctionType
*Ty
, StringRef ConstStr
) {
275 return makeStringError("inline asm cannot be variadic");
277 ConstraintInfoVector Constraints
= ParseConstraints(ConstStr
);
279 // Error parsing constraints.
280 if (Constraints
.empty() && !ConstStr
.empty())
281 return makeStringError("failed to parse constraints");
283 unsigned NumOutputs
= 0, NumInputs
= 0, NumClobbers
= 0;
284 unsigned NumIndirect
= 0, NumLabels
= 0;
286 for (const ConstraintInfo
&Constraint
: Constraints
) {
287 switch (Constraint
.Type
) {
288 case InlineAsm::isOutput
:
289 if ((NumInputs
-NumIndirect
) != 0 || NumClobbers
!= 0 || NumLabels
!= 0)
290 return makeStringError("output constraint occurs after input, "
291 "clobber or label constraint");
293 if (!Constraint
.isIndirect
) {
298 [[fallthrough
]]; // We fall through for Indirect Outputs.
299 case InlineAsm::isInput
:
301 return makeStringError("input constraint occurs after clobber "
305 case InlineAsm::isClobber
:
308 case InlineAsm::isLabel
:
310 return makeStringError("label constraint occurs after clobber "
318 switch (NumOutputs
) {
320 if (!Ty
->getReturnType()->isVoidTy())
321 return makeStringError("inline asm without outputs must return void");
324 if (Ty
->getReturnType()->isStructTy())
325 return makeStringError("inline asm with one output cannot return struct");
328 StructType
*STy
= dyn_cast
<StructType
>(Ty
->getReturnType());
329 if (!STy
|| STy
->getNumElements() != NumOutputs
)
330 return makeStringError("number of output constraints does not match "
331 "number of return struct elements");
335 if (Ty
->getNumParams() != NumInputs
)
336 return makeStringError("number of input constraints does not match number "
339 // We don't have access to labels here, NumLabels will be checked separately.
340 return Error::success();