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,
33 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects,
37 InlineAsm::InlineAsm(const FunctionType *Ty, const StringRef &asmString,
38 const StringRef &constraints, bool hasSideEffects,
40 : Value(PointerType::getUnqual(Ty),
43 Constraints(constraints), HasSideEffects(hasSideEffects),
44 IsAlignStack(isAlignStack) {
46 // Do various checks on the constraint string and type.
47 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
50 const FunctionType *InlineAsm::getFunctionType() const {
51 return cast<FunctionType>(getType()->getElementType());
54 /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
55 /// fields in this structure. If the constraint string is not understood,
56 /// return true, otherwise return false.
57 bool InlineAsm::ConstraintInfo::Parse(const StringRef &Str,
58 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
59 StringRef::iterator I = Str.begin(), E = Str.end();
63 isEarlyClobber = false;
65 isCommutative = false;
72 } else if (*I == '=') {
82 if (I == E) return true; // Just a prefix, like "==" or "~".
84 // Parse the modifiers.
85 bool DoneWithModifiers = false;
86 while (!DoneWithModifiers) {
89 DoneWithModifiers = true;
91 case '&': // Early clobber.
92 if (Type != isOutput || // Cannot early clobber anything but output.
93 isEarlyClobber) // Reject &&&&&&
95 isEarlyClobber = true;
97 case '%': // Commutative.
98 if (Type == isClobber || // Cannot commute clobbers.
99 isCommutative) // Reject %%%%%
101 isCommutative = true;
103 case '#': // Comment.
104 case '*': // Register preferencing.
105 return true; // Not supported.
108 if (!DoneWithModifiers) {
110 if (I == E) return true; // Just prefixes and modifiers!
114 // Parse the various constraints.
116 if (*I == '{') { // Physical register reference.
117 // Find the end of the register name.
118 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
119 if (ConstraintEnd == E) return true; // "{foo"
120 Codes.push_back(std::string(I, ConstraintEnd+1));
122 } else if (isdigit(*I)) { // Matching Constraint
123 // Maximal munch numbers.
124 StringRef::iterator NumStart = I;
125 while (I != E && isdigit(*I))
127 Codes.push_back(std::string(NumStart, I));
128 unsigned N = atoi(Codes.back().c_str());
129 // Check that this is a valid matching constraint!
130 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
132 return true; // Invalid constraint number.
134 // If Operand N already has a matching input, reject this. An output
135 // can't be constrained to the same value as multiple inputs.
136 if (ConstraintsSoFar[N].hasMatchingInput())
139 // Note that operand #n has a matching input.
140 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
142 // Single letter constraint.
143 Codes.push_back(std::string(I, I+1));
151 std::vector<InlineAsm::ConstraintInfo>
152 InlineAsm::ParseConstraints(const StringRef &Constraints) {
153 std::vector<ConstraintInfo> Result;
155 // Scan the constraints string.
156 for (StringRef::iterator I = Constraints.begin(),
157 E = Constraints.end(); I != E; ) {
160 // Find the end of this constraint.
161 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
163 if (ConstraintEnd == I || // Empty constraint like ",,"
164 Info.Parse(std::string(I, ConstraintEnd), Result)) {
165 Result.clear(); // Erroneous constraint?
169 Result.push_back(Info);
171 // ConstraintEnd may be either the next comma or the end of the string. In
172 // the former case, we skip the comma.
176 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
184 /// Verify - Verify that the specified constraint string is reasonable for the
185 /// specified function type, and otherwise validate the constraint string.
186 bool InlineAsm::Verify(const FunctionType *Ty, const StringRef &ConstStr) {
187 if (Ty->isVarArg()) return false;
189 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
191 // Error parsing constraints.
192 if (Constraints.empty() && !ConstStr.empty()) return false;
194 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
195 unsigned NumIndirect = 0;
197 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
198 switch (Constraints[i].Type) {
199 case InlineAsm::isOutput:
200 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
201 return false; // outputs before inputs and clobbers.
202 if (!Constraints[i].isIndirect) {
207 // FALLTHROUGH for Indirect Outputs.
208 case InlineAsm::isInput:
209 if (NumClobbers) return false; // inputs before clobbers.
212 case InlineAsm::isClobber:
218 switch (NumOutputs) {
220 if (Ty->getReturnType() != Type::getVoidTy(Ty->getContext())) return false;
223 if (isa<StructType>(Ty->getReturnType())) return false;
226 const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
227 if (STy == 0 || STy->getNumElements() != NumOutputs)
232 if (Ty->getNumParams() != NumInputs) return false;