From f0b415f178615714de38fb8196f49d131e54274b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 26 Jan 2006 02:21:59 +0000 Subject: [PATCH] add method for constraint parsing git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25637 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/InlineAsm.cpp | 79 +++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/lib/VMCore/InlineAsm.cpp b/lib/VMCore/InlineAsm.cpp index 3ccd8aca7eb..e9a81e4ed88 100644 --- a/lib/VMCore/InlineAsm.cpp +++ b/lib/VMCore/InlineAsm.cpp @@ -38,25 +38,17 @@ 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; +std::vector > +InlineAsm::ParseConstraints(const std::string &Constraints) { + std::vector > Result; // Scan the constraints string. for (std::string::const_iterator I = Constraints.begin(), - E = Constraints.end(); I != E; ) { - if (*I == ',') return false; // Empty constraint like ",," + E = Constraints.end(); I != E; ) { + if (*I == ',') { Result.clear(); break; } // Empty constraint like ",," // Parse the prefix. - enum { - isInput, // 'x' - isOutput, // '=x' - isIndirectOutput, // '==x' - isClobber, // '~x' - } ConstraintType = isInput; + ConstraintPrefix ConstraintType = isInput; if (*I == '~') { ConstraintType = isClobber; @@ -71,9 +63,49 @@ bool InlineAsm::Verify(const FunctionType *Ty, const std::string &Constraints) { } } - if (I == E) return false; // Just a prefix, like "==" or "~". + if (I == E) { Result.clear(); break; } // Just a prefix, like "==" or "~". + + std::string::const_iterator IdStart = I; + + // Parse the id. We accept [a-zA-Z0-9] currently. + while (I != E && isalnum(*I)) ++I; + + if (IdStart == I) { // Requires more than just a prefix + Result.clear(); + break; + } - switch (ConstraintType) { + // Remember this constraint. + Result.push_back(std::make_pair(ConstraintType, std::string(IdStart, 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 != ',') { Result.clear(); break; } + ++I; + if (I == E) { Result.clear(); break; } // don't allow "xyz," + } + } + + return Result; +} + + +/// 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 &ConstStr) { + if (Ty->isVarArg()) return false; + + std::vector > + Constraints = ParseConstraints(ConstStr); + + // Error parsing constraints. + if (Constraints.empty() && !ConstStr.empty()) return false; + + unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0; + + for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { + switch (Constraints[i].first) { case isOutput: if (NumInputs || NumClobbers) return false; // outputs come first. ++NumOutputs; @@ -87,20 +119,9 @@ bool InlineAsm::Verify(const FunctionType *Ty, const std::string &Constraints) { ++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 (NumOutputs > 1) return false; // Only one result allowed so far. if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs) return false; // NumOutputs = 1 iff has a result type. -- 2.34.1