parse and verify the constraint string.
authorChris Lattner <sabre@nondot.org>
Thu, 26 Jan 2006 00:48:33 +0000 (00:48 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 26 Jan 2006 00:48:33 +0000 (00:48 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25631 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/InlineAsm.cpp

index cc817e3795a2fcef0a321a4d1c3c5e64d75e0bc6..3ccd8aca7ebf993759f9bbf8868b5ce9a0ff7aac 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "llvm/InlineAsm.h"
 #include "llvm/DerivedTypes.h"
+#include <cctype>
 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<FunctionType>(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;
 }