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::string::const_iterator I = Str.begin(), E = Str.end();
50 isEarlyClobber = false;
51 isIndirectOutput =false;
57 } else if (*I == '=') {
60 if (I != E && *I == '=') {
61 isIndirectOutput = true;
66 if (I == E) return true; // Just a prefix, like "==" or "~".
68 // Parse the modifiers.
69 bool DoneWithModifiers = false;
70 while (!DoneWithModifiers) {
73 DoneWithModifiers = true;
76 if (Type != isOutput || // Cannot early clobber anything but output.
77 isEarlyClobber) // Reject &&&&&&
79 isEarlyClobber = true;
83 if (!DoneWithModifiers) {
85 if (I == E) return true; // Just prefixes and modifiers!
89 // Parse the various constraints.
91 if (*I == '{') { // Physical register reference.
92 // Find the end of the register name.
93 std::string::const_iterator ConstraintEnd = std::find(I+1, E, '}');
94 if (ConstraintEnd == E) return true; // "{foo"
95 Codes.push_back(std::string(I, ConstraintEnd+1));
97 } else if (isdigit(*I)) {
98 // Maximal munch numbers.
99 std::string::const_iterator NumStart = I;
100 while (I != E && isdigit(*I))
102 Codes.push_back(std::string(NumStart, I));
104 // Single letter constraint.
105 Codes.push_back(std::string(I, I+1));
113 std::vector<InlineAsm::ConstraintInfo>
114 InlineAsm::ParseConstraints(const std::string &Constraints) {
115 std::vector<ConstraintInfo> Result;
117 // Scan the constraints string.
118 for (std::string::const_iterator I = Constraints.begin(),
119 E = Constraints.end(); I != E; ) {
122 // Find the end of this constraint.
123 std::string::const_iterator ConstraintEnd = std::find(I, E, ',');
125 if (ConstraintEnd == I || // Empty constraint like ",,"
126 Info.Parse(std::string(I, ConstraintEnd))) { // Erroneous constraint?
131 Result.push_back(Info);
133 // ConstraintEnd may be either the next comma or the end of the string. In
134 // the former case, we skip the comma.
138 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
146 /// Verify - Verify that the specified constraint string is reasonable for the
147 /// specified function type, and otherwise validate the constraint string.
148 bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
149 if (Ty->isVarArg()) return false;
151 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
153 // Error parsing constraints.
154 if (Constraints.empty() && !ConstStr.empty()) return false;
156 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
158 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
159 switch (Constraints[i].Type) {
160 case InlineAsm::isOutput:
161 if (!Constraints[i].isIndirectOutput) {
162 if (NumInputs || NumClobbers) return false; // outputs come first.
166 // FALLTHROUGH for IndirectOutputs.
167 case InlineAsm::isInput:
168 if (NumClobbers) return false; // inputs before clobbers.
171 case InlineAsm::isClobber:
177 if (NumOutputs > 1) return false; // Only one result allowed so far.
179 if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
180 return false; // NumOutputs = 1 iff has a result type.
182 if (Ty->getNumParams() != NumInputs) return false;