Devirtualizing Value destructor (PR889). Patch by Pawel Kunio!
[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::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
47   std::string::const_iterator I = Str.begin(), E = Str.end();
48   
49   // Initialize
50   Type = isInput;
51   isEarlyClobber = false;
52   hasMatchingInput = false;
53   isCommutative = false;
54   isIndirect = false;
55   
56   // Parse prefixes.
57   if (*I == '~') {
58     Type = isClobber;
59     ++I;
60   } else if (*I == '=') {
61     ++I;
62     Type = isOutput;
63   }
64   
65   if (*I == '*') {
66     isIndirect = true;
67     ++I;
68   }
69   
70   if (I == E) return true;  // Just a prefix, like "==" or "~".
71   
72   // Parse the modifiers.
73   bool DoneWithModifiers = false;
74   while (!DoneWithModifiers) {
75     switch (*I) {
76     default:
77       DoneWithModifiers = true;
78       break;
79     case '&':     // Early clobber.
80       if (Type != isOutput ||      // Cannot early clobber anything but output.
81           isEarlyClobber)          // Reject &&&&&&
82         return true;
83       isEarlyClobber = true;
84       break;
85     case '%':     // Commutative.
86       if (Type == isClobber ||     // Cannot commute clobbers.
87           isCommutative)           // Reject %%%%%
88         return true;
89       isCommutative = true;
90       break;
91     case '#':     // Comment.
92     case '*':     // Register preferencing.
93       return true;     // Not supported.
94     }
95     
96     if (!DoneWithModifiers) {
97       ++I;
98       if (I == E) return true;   // Just prefixes and modifiers!
99     }
100   }
101   
102   // Parse the various constraints.
103   while (I != E) {
104     if (*I == '{') {   // Physical register reference.
105       // Find the end of the register name.
106       std::string::const_iterator ConstraintEnd = std::find(I+1, E, '}');
107       if (ConstraintEnd == E) return true;  // "{foo"
108       Codes.push_back(std::string(I, ConstraintEnd+1));
109       I = ConstraintEnd+1;
110     } else if (isdigit(*I)) {     // Matching Constraint
111       // Maximal munch numbers.
112       std::string::const_iterator NumStart = I;
113       while (I != E && isdigit(*I))
114         ++I;
115       Codes.push_back(std::string(NumStart, I));
116       unsigned N = atoi(Codes.back().c_str());
117       // Check that this is a valid matching constraint!
118       if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
119           Type != isInput)
120         return true;  // Invalid constraint number.
121       
122       // Note that operand #n has a matching input.
123       ConstraintsSoFar[N].hasMatchingInput = true;
124     } else {
125       // Single letter constraint.
126       Codes.push_back(std::string(I, I+1));
127       ++I;
128     }
129   }
130
131   return false;
132 }
133
134 std::vector<InlineAsm::ConstraintInfo>
135 InlineAsm::ParseConstraints(const std::string &Constraints) {
136   std::vector<ConstraintInfo> Result;
137   
138   // Scan the constraints string.
139   for (std::string::const_iterator I = Constraints.begin(), 
140        E = Constraints.end(); I != E; ) {
141     ConstraintInfo Info;
142
143     // Find the end of this constraint.
144     std::string::const_iterator ConstraintEnd = std::find(I, E, ',');
145
146     if (ConstraintEnd == I ||  // Empty constraint like ",,"
147         Info.Parse(std::string(I, ConstraintEnd), Result)) {
148       Result.clear();          // Erroneous constraint?
149       break;
150     }
151
152     Result.push_back(Info);
153     
154     // ConstraintEnd may be either the next comma or the end of the string.  In
155     // the former case, we skip the comma.
156     I = ConstraintEnd;
157     if (I != E) {
158       ++I;
159       if (I == E) { Result.clear(); break; }    // don't allow "xyz,"
160     }
161   }
162   
163   return Result;
164 }
165
166
167 /// Verify - Verify that the specified constraint string is reasonable for the
168 /// specified function type, and otherwise validate the constraint string.
169 bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
170   if (Ty->isVarArg()) return false;
171   
172   std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
173   
174   // Error parsing constraints.
175   if (Constraints.empty() && !ConstStr.empty()) return false;
176   
177   unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
178   
179   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
180     switch (Constraints[i].Type) {
181     case InlineAsm::isOutput:
182       if (!Constraints[i].isIndirect) {
183         if (NumInputs || NumClobbers) return false;  // outputs come first.
184         ++NumOutputs;
185         break;
186       }
187       // FALLTHROUGH for Indirect Outputs.
188     case InlineAsm::isInput:
189       if (NumClobbers) return false;               // inputs before clobbers.
190       ++NumInputs;
191       break;
192     case InlineAsm::isClobber:
193       ++NumClobbers;
194       break;
195     }
196   }
197     
198   if (NumOutputs > 1) return false;  // Only one result allowed so far.
199   
200   if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs)
201     return false;   // NumOutputs = 1 iff has a result type.
202   
203   if (Ty->getNumParams() != NumInputs) return false;
204   return true;
205 }
206
207 DEFINING_FILE_FOR(InlineAsm)