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 std::vector<std::pair<InlineAsm::ConstraintPrefix, std::string> >
42 InlineAsm::ParseConstraints(const std::string &Constraints) {
43 std::vector<std::pair<InlineAsm::ConstraintPrefix, std::string> > Result;
45 // Scan the constraints string.
46 for (std::string::const_iterator I = Constraints.begin(),
47 E = Constraints.end(); I != E; ) {
48 if (*I == ',') { Result.clear(); break; } // Empty constraint like ",,"
51 ConstraintPrefix ConstraintType = isInput;
54 ConstraintType = isClobber;
56 } else if (*I == '=') {
58 if (I != E && *I == '=') {
59 ConstraintType = isIndirectOutput;
62 ConstraintType = isOutput;
66 if (I == E) { Result.clear(); break; } // Just a prefix, like "==" or "~".
68 std::string::const_iterator IdStart = I;
70 // Parse the id. We accept [a-zA-Z0-9] currently.
71 while (I != E && isalnum(*I)) ++I;
73 if (IdStart == I) { // Requires more than just a prefix
78 // Remember this constraint.
79 Result.push_back(std::make_pair(ConstraintType, std::string(IdStart, I)));
81 // If we reached the end of the ID, we must have the end of the string or a
82 // comma, which we skip now.
84 if (*I != ',') { Result.clear(); break; }
86 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
94 /// Verify - Verify that the specified constraint string is reasonable for the
95 /// specified function type, and otherwise validate the constraint string.
96 bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
97 if (Ty->isVarArg()) return false;
99 std::vector<std::pair<ConstraintPrefix, std::string> >
100 Constraints = ParseConstraints(ConstStr);
102 // Error parsing constraints.
103 if (Constraints.empty() && !ConstStr.empty()) return false;
105 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
107 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
108 switch (Constraints[i].first) {
110 if (NumInputs || NumClobbers) return false; // outputs come first.
114 case isIndirectOutput:
115 if (NumClobbers) return false; // inputs before clobbers.
124 if (NumOutputs > 1) return false; // Only one result allowed so far.
126 if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
127 return false; // NumOutputs = 1 iff has a result type.
129 if (Ty->getNumParams() != NumInputs) return false;