1 //===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source 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 // NOTE: when memoizing the function type, we have to be careful to handle the
21 // case when the type gets refined.
23 InlineAsm *InlineAsm::get(const FunctionType *Ty, const std::string &AsmString,
24 const std::string &Constraints, bool hasSideEffects) {
26 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);
29 InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
30 const std::string &constraints, bool hasSideEffects)
31 : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString),
32 Constraints(constraints), HasSideEffects(hasSideEffects) {
34 // Do various checks on the constraint string and type.
35 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
38 const FunctionType *InlineAsm::getFunctionType() const {
39 return cast<FunctionType>(getType()->getElementType());
42 /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
43 /// fields in this structure. If the constraint string is not understood,
44 /// return true, otherwise return false.
45 bool InlineAsm::ConstraintInfo::Parse(const std::string &Str,
46 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
47 std::string::const_iterator I = Str.begin(), E = Str.end();
51 isEarlyClobber = false;
52 hasMatchingInput = false;
53 isCommutative = false;
60 } else if (*I == '=') {
70 if (I == E) return true; // Just a prefix, like "==" or "~".
72 // Parse the modifiers.
73 bool DoneWithModifiers = false;
74 while (!DoneWithModifiers) {
77 DoneWithModifiers = true;
79 case '&': // Early clobber.
80 if (Type != isOutput || // Cannot early clobber anything but output.
81 isEarlyClobber) // Reject &&&&&&
83 isEarlyClobber = true;
85 case '%': // Commutative.
86 if (Type == isClobber || // Cannot commute clobbers.
87 isCommutative) // Reject %%%%%
92 case '*': // Register preferencing.
93 return true; // Not supported.
96 if (!DoneWithModifiers) {
98 if (I == E) return true; // Just prefixes and modifiers!
102 // Parse the various constraints.
104 if (*I == '{') { // Physical register reference.
105 // Find the end of the register name.
106 std::string::const_iterator ConstraintEnd = std::find(I+1, E, '}');
107 if (ConstraintEnd == E) return true; // "{foo"
108 Codes.push_back(std::string(I, ConstraintEnd+1));
110 } else if (isdigit(*I)) { // Matching Constraint
111 // Maximal munch numbers.
112 std::string::const_iterator NumStart = I;
113 while (I != E && isdigit(*I))
115 Codes.push_back(std::string(NumStart, I));
116 unsigned N = atoi(Codes.back().c_str());
117 // Check that this is a valid matching constraint!
118 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
120 return true; // Invalid constraint number.
122 // Note that operand #n has a matching input.
123 ConstraintsSoFar[N].hasMatchingInput = true;
125 // Single letter constraint.
126 Codes.push_back(std::string(I, I+1));
134 std::vector<InlineAsm::ConstraintInfo>
135 InlineAsm::ParseConstraints(const std::string &Constraints) {
136 std::vector<ConstraintInfo> Result;
138 // Scan the constraints string.
139 for (std::string::const_iterator I = Constraints.begin(),
140 E = Constraints.end(); I != E; ) {
143 // Find the end of this constraint.
144 std::string::const_iterator ConstraintEnd = std::find(I, E, ',');
146 if (ConstraintEnd == I || // Empty constraint like ",,"
147 Info.Parse(std::string(I, ConstraintEnd), Result)) {
148 Result.clear(); // Erroneous constraint?
152 Result.push_back(Info);
154 // ConstraintEnd may be either the next comma or the end of the string. In
155 // the former case, we skip the comma.
159 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
167 /// Verify - Verify that the specified constraint string is reasonable for the
168 /// specified function type, and otherwise validate the constraint string.
169 bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
170 if (Ty->isVarArg()) return false;
172 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
174 // Error parsing constraints.
175 if (Constraints.empty() && !ConstStr.empty()) return false;
177 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
179 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
180 switch (Constraints[i].Type) {
181 case InlineAsm::isOutput:
182 if (!Constraints[i].isIndirect) {
183 if (NumInputs || NumClobbers) return false; // outputs come first.
187 // FALLTHROUGH for Indirect Outputs.
188 case InlineAsm::isInput:
189 if (NumClobbers) return false; // inputs before clobbers.
192 case InlineAsm::isClobber:
198 if (NumOutputs > 1) return false; // Only one result allowed so far.
200 if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
201 return false; // NumOutputs = 1 iff has a result type.
203 if (Ty->getNumParams() != NumInputs) return false;
207 DEFINING_FILE_FOR(InlineAsm)