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 std::string &AsmString,
30 const std::string &Constraints, bool hasSideEffects) {
32 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);
35 InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
36 const std::string &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 std::string &Str,
54 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
55 std::string::const_iterator I = Str.begin(), E = Str.end();
59 isEarlyClobber = false;
60 hasMatchingInput = 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 std::string::const_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 std::string::const_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 // Note that operand #n has a matching input.
131 ConstraintsSoFar[N].hasMatchingInput = true;
133 // Single letter constraint.
134 Codes.push_back(std::string(I, I+1));
142 std::vector<InlineAsm::ConstraintInfo>
143 InlineAsm::ParseConstraints(const std::string &Constraints) {
144 std::vector<ConstraintInfo> Result;
146 // Scan the constraints string.
147 for (std::string::const_iterator I = Constraints.begin(),
148 E = Constraints.end(); I != E; ) {
151 // Find the end of this constraint.
152 std::string::const_iterator ConstraintEnd = std::find(I, E, ',');
154 if (ConstraintEnd == I || // Empty constraint like ",,"
155 Info.Parse(std::string(I, ConstraintEnd), Result)) {
156 Result.clear(); // Erroneous constraint?
160 Result.push_back(Info);
162 // ConstraintEnd may be either the next comma or the end of the string. In
163 // the former case, we skip the comma.
167 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
175 /// Verify - Verify that the specified constraint string is reasonable for the
176 /// specified function type, and otherwise validate the constraint string.
177 bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
178 if (Ty->isVarArg()) return false;
180 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
182 // Error parsing constraints.
183 if (Constraints.empty() && !ConstStr.empty()) return false;
185 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
187 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
188 switch (Constraints[i].Type) {
189 case InlineAsm::isOutput:
190 if (!Constraints[i].isIndirect) {
191 if (NumInputs || NumClobbers) return false; // outputs come first.
195 // FALLTHROUGH for Indirect Outputs.
196 case InlineAsm::isInput:
197 if (NumClobbers) return false; // inputs before clobbers.
200 case InlineAsm::isClobber:
206 switch (NumOutputs) {
208 if (Ty->getReturnType() != Type::VoidTy) return false;
211 if (isa<StructType>(Ty->getReturnType())) return false;
214 const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
215 if (STy == 0 || STy->getNumElements() != NumOutputs)
220 if (Ty->getNumParams() != NumInputs) return false;