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"
19 // NOTE: when memoizing the function type, we have to be careful to handle the
20 // case when the type gets refined.
22 InlineAsm *InlineAsm::get(const FunctionType *Ty, const std::string &AsmString,
23 const std::string &Constraints, bool hasSideEffects) {
25 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);
28 InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
29 const std::string &constraints, bool hasSideEffects)
30 : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString),
31 Constraints(constraints), HasSideEffects(hasSideEffects) {
33 // Do various checks on the constraint string and type.
34 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
37 const FunctionType *InlineAsm::getFunctionType() const {
38 return cast<FunctionType>(getType()->getElementType());
41 /// Verify - Verify that the specified constraint string is reasonable for the
42 /// specified function type, and otherwise validate the constraint string.
43 bool InlineAsm::Verify(const FunctionType *Ty, const std::string &Constraints) {
44 if (Ty->isVarArg()) return false;
46 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
48 // Scan the constraints string.
49 for (std::string::const_iterator I = Constraints.begin(),
50 E = Constraints.end(); I != E; ) {
51 if (*I == ',') return false; // Empty constraint like ",,"
57 isIndirectOutput, // '==x'
59 } ConstraintType = isInput;
62 ConstraintType = isClobber;
64 } else if (*I == '=') {
66 if (I != E && *I == '=') {
67 ConstraintType = isIndirectOutput;
70 ConstraintType = isOutput;
74 if (I == E) return false; // Just a prefix, like "==" or "~".
76 switch (ConstraintType) {
78 if (NumInputs || NumClobbers) return false; // outputs come first.
82 case isIndirectOutput:
83 if (NumClobbers) return false; // inputs before clobbers.
91 // Parse the id. We accept [a-zA-Z0-9] currently.
92 while (I != E && isalnum(*I)) ++I;
94 // If we reached the end of the ID, we must have the end of the string or a
95 // comma, which we skip now.
97 if (*I != ',') return false;
99 if (I == E) return false; // don't allow "xyz,"
103 if (NumOutputs > 1) return false; // Only one result allowed.
105 if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
106 return false; // NumOutputs = 1 iff has a result type.
108 if (Ty->getNumParams() != NumInputs) return false;