From: Chris Lattner Date: Thu, 26 Jan 2006 00:48:33 +0000 (+0000) Subject: parse and verify the constraint string. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=3b91778659ec7d515ae1354022f0213e5de64d80;p=oota-llvm.git parse and verify the constraint string. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25631 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/VMCore/InlineAsm.cpp b/lib/VMCore/InlineAsm.cpp index cc817e3795a..3ccd8aca7eb 100644 --- a/lib/VMCore/InlineAsm.cpp +++ b/lib/VMCore/InlineAsm.cpp @@ -13,6 +13,7 @@ #include "llvm/InlineAsm.h" #include "llvm/DerivedTypes.h" +#include using namespace llvm; // NOTE: when memoizing the function type, we have to be careful to handle the @@ -37,6 +38,73 @@ const FunctionType *InlineAsm::getFunctionType() const { return cast(getType()->getElementType()); } +/// Verify - Verify that the specified constraint string is reasonable for the +/// specified function type, and otherwise validate the constraint string. bool InlineAsm::Verify(const FunctionType *Ty, const std::string &Constraints) { + if (Ty->isVarArg()) return false; + + unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0; + + // Scan the constraints string. + for (std::string::const_iterator I = Constraints.begin(), + E = Constraints.end(); I != E; ) { + if (*I == ',') return false; // Empty constraint like ",," + + // Parse the prefix. + enum { + isInput, // 'x' + isOutput, // '=x' + isIndirectOutput, // '==x' + isClobber, // '~x' + } ConstraintType = isInput; + + if (*I == '~') { + ConstraintType = isClobber; + ++I; + } else if (*I == '=') { + ++I; + if (I != E && *I == '=') { + ConstraintType = isIndirectOutput; + ++I; + } else { + ConstraintType = isOutput; + } + } + + if (I == E) return false; // Just a prefix, like "==" or "~". + + switch (ConstraintType) { + case isOutput: + if (NumInputs || NumClobbers) return false; // outputs come first. + ++NumOutputs; + break; + case isInput: + case isIndirectOutput: + if (NumClobbers) return false; // inputs before clobbers. + ++NumInputs; + break; + case isClobber: + ++NumClobbers; + break; + } + + // Parse the id. We accept [a-zA-Z0-9] currently. + while (I != E && isalnum(*I)) ++I; + + // If we reached the end of the ID, we must have the end of the string or a + // comma, which we skip now. + if (I != E) { + if (*I != ',') return false; + ++I; + if (I == E) return false; // don't allow "xyz," + } + } + + if (NumOutputs > 1) return false; // Only one result allowed. + + if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs) + return false; // NumOutputs = 1 iff has a result type. + + if (Ty->getNumParams() != NumInputs) return false; return true; }