Compile this:
[oota-llvm.git] / lib / VMCore / InlineAsm.cpp
1 //===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the InlineAsm class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/InlineAsm.h"
15 #include "llvm/DerivedTypes.h"
16 #include <cctype>
17 using namespace llvm;
18
19 // NOTE: when memoizing the function type, we have to be careful to handle the
20 // case when the type gets refined.
21
22 InlineAsm *InlineAsm::get(const FunctionType *Ty, const std::string &AsmString,
23                           const std::string &Constraints, bool hasSideEffects) {
24   // FIXME: memoize!
25   return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);  
26 }
27
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) {
32
33   // Do various checks on the constraint string and type.
34   assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
35 }
36
37 const FunctionType *InlineAsm::getFunctionType() const {
38   return cast<FunctionType>(getType()->getElementType());
39 }
40
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;
44   
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 ",,"
49     
50     // Parse the prefix.
51     ConstraintPrefix ConstraintType = isInput;
52     
53     if (*I == '~') {
54       ConstraintType = isClobber;
55       ++I;
56     } else if (*I == '=') {
57       ++I;
58       if (I != E && *I == '=') {
59         ConstraintType = isIndirectOutput;
60         ++I;
61       } else {
62         ConstraintType = isOutput;
63       }
64     }
65     
66     if (I == E) { Result.clear(); break; }  // Just a prefix, like "==" or "~".
67     
68     std::string::const_iterator IdStart = I;
69       
70     // Parse the id.  We accept [a-zA-Z0-9] currently.
71     while (I != E && isalnum(*I)) ++I;
72     
73     if (IdStart == I) {                    // Requires more than just a prefix
74       Result.clear();
75       break;
76     }
77     
78     // Remember this constraint.
79     Result.push_back(std::make_pair(ConstraintType, std::string(IdStart, I)));
80     
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.
83     if (I != E) {
84       if (*I != ',') { Result.clear(); break; }
85       ++I;
86       if (I == E) { Result.clear(); break; }    // don't allow "xyz,"
87     }
88   }
89   
90   return Result;
91 }
92
93
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;
98   
99   std::vector<std::pair<ConstraintPrefix, std::string> >
100   Constraints = ParseConstraints(ConstStr);
101   
102   // Error parsing constraints.
103   if (Constraints.empty() && !ConstStr.empty()) return false;
104   
105   unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
106   
107   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
108     switch (Constraints[i].first) {
109     case isOutput:
110       if (NumInputs || NumClobbers) return false;  // outputs come first.
111       ++NumOutputs;
112       break;
113     case isInput:
114     case isIndirectOutput:
115       if (NumClobbers) return false;               // inputs before clobbers.
116       ++NumInputs;
117       break;
118     case isClobber:
119       ++NumClobbers;
120       break;
121     }
122   }
123     
124   if (NumOutputs > 1) return false;  // Only one result allowed so far.
125   
126   if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
127     return false;   // NumOutputs = 1 iff has a result type.
128   
129   if (Ty->getNumParams() != NumInputs) return false;
130   return true;
131 }