1 //===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the InlineAsm class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/InlineAsm.h"
15 #include "llvm/DerivedTypes.h"
20 // Implement the first virtual method in this class in this file so the
21 // InlineAsm vtable is emitted here.
22 InlineAsm::~InlineAsm() {
26 // NOTE: when memoizing the function type, we have to be careful to handle the
27 // case when the type gets refined.
29 InlineAsm
*InlineAsm::get(const FunctionType
*Ty
, const StringRef
&AsmString
,
30 const StringRef
&Constraints
, bool hasSideEffects
) {
32 return new InlineAsm(Ty
, AsmString
, Constraints
, hasSideEffects
);
35 InlineAsm::InlineAsm(const FunctionType
*Ty
, const StringRef
&asmString
,
36 const StringRef
&constraints
, bool hasSideEffects
)
37 : Value(PointerType::getUnqual(Ty
),
40 Constraints(constraints
), HasSideEffects(hasSideEffects
) {
42 // Do various checks on the constraint string and type.
43 assert(Verify(Ty
, constraints
) && "Function type not legal for constraints!");
46 const FunctionType
*InlineAsm::getFunctionType() const {
47 return cast
<FunctionType
>(getType()->getElementType());
50 /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
51 /// fields in this structure. If the constraint string is not understood,
52 /// return true, otherwise return false.
53 bool InlineAsm::ConstraintInfo::Parse(const StringRef
&Str
,
54 std::vector
<InlineAsm::ConstraintInfo
> &ConstraintsSoFar
) {
55 StringRef::iterator I
= Str
.begin(), E
= Str
.end();
59 isEarlyClobber
= false;
61 isCommutative
= false;
68 } else if (*I
== '=') {
78 if (I
== E
) return true; // Just a prefix, like "==" or "~".
80 // Parse the modifiers.
81 bool DoneWithModifiers
= false;
82 while (!DoneWithModifiers
) {
85 DoneWithModifiers
= true;
87 case '&': // Early clobber.
88 if (Type
!= isOutput
|| // Cannot early clobber anything but output.
89 isEarlyClobber
) // Reject &&&&&&
91 isEarlyClobber
= true;
93 case '%': // Commutative.
94 if (Type
== isClobber
|| // Cannot commute clobbers.
95 isCommutative
) // Reject %%%%%
100 case '*': // Register preferencing.
101 return true; // Not supported.
104 if (!DoneWithModifiers
) {
106 if (I
== E
) return true; // Just prefixes and modifiers!
110 // Parse the various constraints.
112 if (*I
== '{') { // Physical register reference.
113 // Find the end of the register name.
114 StringRef::iterator ConstraintEnd
= std::find(I
+1, E
, '}');
115 if (ConstraintEnd
== E
) return true; // "{foo"
116 Codes
.push_back(std::string(I
, ConstraintEnd
+1));
118 } else if (isdigit(*I
)) { // Matching Constraint
119 // Maximal munch numbers.
120 StringRef::iterator NumStart
= I
;
121 while (I
!= E
&& isdigit(*I
))
123 Codes
.push_back(std::string(NumStart
, I
));
124 unsigned N
= atoi(Codes
.back().c_str());
125 // Check that this is a valid matching constraint!
126 if (N
>= ConstraintsSoFar
.size() || ConstraintsSoFar
[N
].Type
!= isOutput
||
128 return true; // Invalid constraint number.
130 // If Operand N already has a matching input, reject this. An output
131 // can't be constrained to the same value as multiple inputs.
132 if (ConstraintsSoFar
[N
].hasMatchingInput())
135 // Note that operand #n has a matching input.
136 ConstraintsSoFar
[N
].MatchingInput
= ConstraintsSoFar
.size();
138 // Single letter constraint.
139 Codes
.push_back(std::string(I
, I
+1));
147 std::vector
<InlineAsm::ConstraintInfo
>
148 InlineAsm::ParseConstraints(const StringRef
&Constraints
) {
149 std::vector
<ConstraintInfo
> Result
;
151 // Scan the constraints string.
152 for (StringRef::iterator I
= Constraints
.begin(),
153 E
= Constraints
.end(); I
!= E
; ) {
156 // Find the end of this constraint.
157 StringRef::iterator ConstraintEnd
= std::find(I
, E
, ',');
159 if (ConstraintEnd
== I
|| // Empty constraint like ",,"
160 Info
.Parse(std::string(I
, ConstraintEnd
), Result
)) {
161 Result
.clear(); // Erroneous constraint?
165 Result
.push_back(Info
);
167 // ConstraintEnd may be either the next comma or the end of the string. In
168 // the former case, we skip the comma.
172 if (I
== E
) { Result
.clear(); break; } // don't allow "xyz,"
180 /// Verify - Verify that the specified constraint string is reasonable for the
181 /// specified function type, and otherwise validate the constraint string.
182 bool InlineAsm::Verify(const FunctionType
*Ty
, const StringRef
&ConstStr
) {
183 if (Ty
->isVarArg()) return false;
185 std::vector
<ConstraintInfo
> Constraints
= ParseConstraints(ConstStr
);
187 // Error parsing constraints.
188 if (Constraints
.empty() && !ConstStr
.empty()) return false;
190 unsigned NumOutputs
= 0, NumInputs
= 0, NumClobbers
= 0;
191 unsigned NumIndirect
= 0;
193 for (unsigned i
= 0, e
= Constraints
.size(); i
!= e
; ++i
) {
194 switch (Constraints
[i
].Type
) {
195 case InlineAsm::isOutput
:
196 if ((NumInputs
-NumIndirect
) != 0 || NumClobbers
!= 0)
197 return false; // outputs before inputs and clobbers.
198 if (!Constraints
[i
].isIndirect
) {
203 // FALLTHROUGH for Indirect Outputs.
204 case InlineAsm::isInput
:
205 if (NumClobbers
) return false; // inputs before clobbers.
208 case InlineAsm::isClobber
:
214 switch (NumOutputs
) {
216 if (Ty
->getReturnType() != Type::VoidTy
) return false;
219 if (isa
<StructType
>(Ty
->getReturnType())) return false;
222 const StructType
*STy
= dyn_cast
<StructType
>(Ty
->getReturnType());
223 if (STy
== 0 || STy
->getNumElements() != NumOutputs
)
228 if (Ty
->getNumParams() != NumInputs
) return false;