Fix VC++ compilation error.
[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 <algorithm>
17 #include <cctype>
18 using namespace llvm;
19
20 // NOTE: when memoizing the function type, we have to be careful to handle the
21 // case when the type gets refined.
22
23 InlineAsm *InlineAsm::get(const FunctionType *Ty, const std::string &AsmString,
24                           const std::string &Constraints, bool hasSideEffects) {
25   // FIXME: memoize!
26   return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);  
27 }
28
29 InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
30                      const std::string &constraints, bool hasSideEffects)
31   : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString), 
32     Constraints(constraints), HasSideEffects(hasSideEffects) {
33
34   // Do various checks on the constraint string and type.
35   assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
36 }
37
38 const FunctionType *InlineAsm::getFunctionType() const {
39   return cast<FunctionType>(getType()->getElementType());
40 }
41
42 /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
43 /// fields in this structure.  If the constraint string is not understood,
44 /// return true, otherwise return false.
45 bool InlineAsm::ConstraintInfo::Parse(const std::string &Str) {
46   std::string::const_iterator I = Str.begin(), E = Str.end();
47   
48   // Initialize
49   Type = isInput;
50   isEarlyClobber = false;
51   isIndirectOutput =false;
52   
53   // Parse the prefix.
54   if (*I == '~') {
55     Type = isClobber;
56     ++I;
57   } else if (*I == '=') {
58     ++I;
59     Type = isOutput;
60     if (I != E && *I == '=') {
61       isIndirectOutput = true;
62       ++I;
63     }
64   }
65   
66   if (I == E) return true;  // Just a prefix, like "==" or "~".
67   
68   // Parse the modifiers.
69   bool DoneWithModifiers = false;
70   while (!DoneWithModifiers) {
71     switch (*I) {
72     default:
73       DoneWithModifiers = true;
74       break;
75     case '&':
76       if (Type != isOutput ||      // Cannot early clobber anything but output.
77           isEarlyClobber)          // Reject &&&&&&
78         return true;
79       isEarlyClobber = true;
80       break;
81     }
82     
83     if (!DoneWithModifiers) {
84       ++I;
85       if (I == E) return true;   // Just prefixes and modifiers!
86     }
87   }
88   
89   // Parse the various constraints.
90   while (I != E) {
91     if (*I == '{') {   // Physical register reference.
92       // Find the end of the register name.
93       std::string::const_iterator ConstraintEnd = std::find(I+1, E, '}');
94       if (ConstraintEnd == E) return true;  // "{foo"
95       Codes.push_back(std::string(I, ConstraintEnd+1));
96       I = ConstraintEnd+1;
97     } else if (isdigit(*I)) {
98       // Maximal munch numbers.
99       std::string::const_iterator NumStart = I;
100       while (I != E && isdigit(*I))
101         ++I;
102       Codes.push_back(std::string(NumStart, I));
103     } else {
104       // Single letter constraint.
105       Codes.push_back(std::string(I, I+1));
106       ++I;
107     }
108   }
109
110   return false;
111 }
112
113 std::vector<InlineAsm::ConstraintInfo>
114 InlineAsm::ParseConstraints(const std::string &Constraints) {
115   std::vector<ConstraintInfo> Result;
116   
117   // Scan the constraints string.
118   for (std::string::const_iterator I = Constraints.begin(), 
119        E = Constraints.end(); I != E; ) {
120     ConstraintInfo Info;
121
122     // Find the end of this constraint.
123     std::string::const_iterator ConstraintEnd = std::find(I, E, ',');
124
125     if (ConstraintEnd == I ||  // Empty constraint like ",,"
126         Info.Parse(std::string(I, ConstraintEnd))) {   // Erroneous constraint?
127       Result.clear();
128       break;
129     }
130
131     Result.push_back(Info);
132     
133     // ConstraintEnd may be either the next comma or the end of the string.  In
134     // the former case, we skip the comma.
135     I = ConstraintEnd;
136     if (I != E) {
137       ++I;
138       if (I == E) { Result.clear(); break; }    // don't allow "xyz,"
139     }
140   }
141   
142   return Result;
143 }
144
145
146 /// Verify - Verify that the specified constraint string is reasonable for the
147 /// specified function type, and otherwise validate the constraint string.
148 bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
149   if (Ty->isVarArg()) return false;
150   
151   std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
152   
153   // Error parsing constraints.
154   if (Constraints.empty() && !ConstStr.empty()) return false;
155   
156   unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
157   
158   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
159     switch (Constraints[i].Type) {
160     case InlineAsm::isOutput:
161       if (!Constraints[i].isIndirectOutput) {
162         if (NumInputs || NumClobbers) return false;  // outputs come first.
163         ++NumOutputs;
164         break;
165       }
166       // FALLTHROUGH for IndirectOutputs.
167     case InlineAsm::isInput:
168       if (NumClobbers) return false;               // inputs before clobbers.
169       ++NumInputs;
170       break;
171     case InlineAsm::isClobber:
172       ++NumClobbers;
173       break;
174     }
175   }
176     
177   if (NumOutputs > 1) return false;  // Only one result allowed so far.
178   
179   if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
180     return false;   // NumOutputs = 1 iff has a result type.
181   
182   if (Ty->getNumParams() != NumInputs) return false;
183   return true;
184 }