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