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