blob: 814b31aeb386eb7415051c05864e6a5f3e1e2c54 [file] [log] [blame]
Chris Lattnereef2fe72006-01-24 04:13:11 +00001//===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnereef2fe72006-01-24 04:13:11 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the InlineAsm class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/InlineAsm.h"
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000015#include "ConstantsContext.h"
16#include "LLVMContextImpl.h"
Chris Lattnereef2fe72006-01-24 04:13:11 +000017#include "llvm/DerivedTypes.h"
Jeff Cohenb24b66f2006-02-01 04:37:04 +000018#include <algorithm>
Chris Lattner8547e3a2006-01-26 00:48:33 +000019#include <cctype>
Chris Lattnereef2fe72006-01-24 04:13:11 +000020using namespace llvm;
21
Gordon Henriksen14a55692007-12-10 02:14:30 +000022// Implement the first virtual method in this class in this file so the
23// InlineAsm vtable is emitted here.
24InlineAsm::~InlineAsm() {
25}
26
27
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000028InlineAsm *InlineAsm::get(const FunctionType *Ty, StringRef AsmString,
29 StringRef Constraints, bool hasSideEffects,
Dale Johannesen1cfb9582009-10-21 23:28:00 +000030 bool isAlignStack) {
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000031 InlineAsmKeyType Key(AsmString, Constraints, hasSideEffects, isAlignStack);
32 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
33 return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(Ty), Key);
Chris Lattnera2d810d2006-01-25 22:26:05 +000034}
35
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000036InlineAsm::InlineAsm(const PointerType *Ty, const std::string &asmString,
37 const std::string &constraints, bool hasSideEffects,
Dale Johannesen1cfb9582009-10-21 23:28:00 +000038 bool isAlignStack)
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000039 : Value(Ty, Value::InlineAsmVal),
Christopher Lambedf07882007-12-17 01:12:55 +000040 AsmString(asmString),
Dale Johannesen1cfb9582009-10-21 23:28:00 +000041 Constraints(constraints), HasSideEffects(hasSideEffects),
42 IsAlignStack(isAlignStack) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000043
Chris Lattnera2d810d2006-01-25 22:26:05 +000044 // Do various checks on the constraint string and type.
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000045 assert(Verify(getFunctionType(), constraints) &&
46 "Function type not legal for constraints!");
47}
48
49void InlineAsm::destroyConstant() {
50 delete this;
Chris Lattnereef2fe72006-01-24 04:13:11 +000051}
52
53const FunctionType *InlineAsm::getFunctionType() const {
54 return cast<FunctionType>(getType()->getElementType());
55}
John Thompson1094c802010-09-13 18:15:37 +000056
57///Default constructor.
58InlineAsm::ConstraintInfo::ConstraintInfo() :
59 isMultipleAlternative(false),
60 Type(isInput), isEarlyClobber(false),
61 MatchingInput(-1), isCommutative(false),
62 isIndirect(false), currentAlternativeIndex(0) {
63}
64
65/// Copy constructor.
66InlineAsm::ConstraintInfo::ConstraintInfo(const ConstraintInfo &other) :
67 isMultipleAlternative(other.isMultipleAlternative),
68 Type(other.Type), isEarlyClobber(other.isEarlyClobber),
69 MatchingInput(other.MatchingInput), isCommutative(other.isCommutative),
70 isIndirect(other.isIndirect), Codes(other.Codes),
71 multipleAlternatives(other.multipleAlternatives),
72 currentAlternativeIndex(other.currentAlternativeIndex) {
73}
Chris Lattnera2d810d2006-01-25 22:26:05 +000074
Chris Lattner7ed31012006-02-01 01:29:47 +000075/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
76/// fields in this structure. If the constraint string is not understood,
77/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000078bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
Chris Lattner2f34a9e2006-02-02 00:23:53 +000079 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000080 StringRef::iterator I = Str.begin(), E = Str.end();
John Thompson1094c802010-09-13 18:15:37 +000081 unsigned multipleAlternativeCount = Str.count('|') + 1;
82 unsigned multipleAlternativeIndex = 0;
83 std::vector<std::string> *pCodes = &Codes;
Chris Lattner7ed31012006-02-01 01:29:47 +000084
85 // Initialize
John Thompson1094c802010-09-13 18:15:37 +000086 isMultipleAlternative = (multipleAlternativeCount > 1 ? true : false);
87 if (isMultipleAlternative) {
88 multipleAlternatives.resize(multipleAlternativeCount);
89 pCodes = &multipleAlternatives[0].Codes;
90 }
Chris Lattner7ed31012006-02-01 01:29:47 +000091 Type = isInput;
92 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000093 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000094 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000095 isIndirect = false;
John Thompson1094c802010-09-13 18:15:37 +000096 currentAlternativeIndex = 0;
Chris Lattner7ed31012006-02-01 01:29:47 +000097
Chris Lattnerc48e2c32007-04-28 01:02:58 +000098 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000099 if (*I == '~') {
100 Type = isClobber;
101 ++I;
102 } else if (*I == '=') {
103 ++I;
104 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000105 }
106
107 if (*I == '*') {
108 isIndirect = true;
109 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000110 }
111
112 if (I == E) return true; // Just a prefix, like "==" or "~".
113
114 // Parse the modifiers.
115 bool DoneWithModifiers = false;
116 while (!DoneWithModifiers) {
117 switch (*I) {
118 default:
119 DoneWithModifiers = true;
120 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000121 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +0000122 if (Type != isOutput || // Cannot early clobber anything but output.
123 isEarlyClobber) // Reject &&&&&&
124 return true;
125 isEarlyClobber = true;
126 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000127 case '%': // Commutative.
128 if (Type == isClobber || // Cannot commute clobbers.
129 isCommutative) // Reject %%%%%
130 return true;
131 isCommutative = true;
132 break;
133 case '#': // Comment.
134 case '*': // Register preferencing.
135 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000136 }
137
138 if (!DoneWithModifiers) {
139 ++I;
140 if (I == E) return true; // Just prefixes and modifiers!
141 }
142 }
143
144 // Parse the various constraints.
145 while (I != E) {
146 if (*I == '{') { // Physical register reference.
147 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000148 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000149 if (ConstraintEnd == E) return true; // "{foo"
John Thompson1094c802010-09-13 18:15:37 +0000150 pCodes->push_back(std::string(I, ConstraintEnd+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000151 I = ConstraintEnd+1;
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000152 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000153 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000154 StringRef::iterator NumStart = I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000155 while (I != E && isdigit(*I))
156 ++I;
John Thompson1094c802010-09-13 18:15:37 +0000157 pCodes->push_back(std::string(NumStart, I));
158 unsigned N = atoi(pCodes->back().c_str());
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000159 // Check that this is a valid matching constraint!
160 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
161 Type != isInput)
162 return true; // Invalid constraint number.
163
Chris Lattner860df6e2008-10-17 16:47:46 +0000164 // If Operand N already has a matching input, reject this. An output
165 // can't be constrained to the same value as multiple inputs.
John Thompson1094c802010-09-13 18:15:37 +0000166 if (isMultipleAlternative) {
167 InlineAsm::SubConstraintInfo &scInfo =
168 ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
169 if (scInfo.MatchingInput != -1)
170 return true;
171 // Note that operand #n has a matching input.
172 scInfo.MatchingInput = ConstraintsSoFar.size();
173 } else {
174 if (ConstraintsSoFar[N].hasMatchingInput())
175 return true;
176 // Note that operand #n has a matching input.
177 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
178 }
179 } else if (*I == '|') {
180 multipleAlternativeIndex++;
181 pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
182 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000183 } else {
184 // Single letter constraint.
John Thompson1094c802010-09-13 18:15:37 +0000185 pCodes->push_back(std::string(I, I+1));
Chris Lattner7ed31012006-02-01 01:29:47 +0000186 ++I;
187 }
188 }
189
190 return false;
191}
192
John Thompson1094c802010-09-13 18:15:37 +0000193/// selectAlternative - Point this constraint to the alternative constraint
194/// indicated by the index.
195void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
196 if (index < multipleAlternatives.size()) {
197 currentAlternativeIndex = index;
198 InlineAsm::SubConstraintInfo &scInfo =
199 multipleAlternatives[currentAlternativeIndex];
200 MatchingInput = scInfo.MatchingInput;
201 Codes = scInfo.Codes;
202 }
203}
204
Chris Lattner7ed31012006-02-01 01:29:47 +0000205std::vector<InlineAsm::ConstraintInfo>
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000206InlineAsm::ParseConstraints(StringRef Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000207 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000208
209 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000210 for (StringRef::iterator I = Constraints.begin(),
211 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000212 ConstraintInfo Info;
213
214 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000215 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000216
217 if (ConstraintEnd == I || // Empty constraint like ",,"
Benjamin Kramer5aaf6772010-07-25 23:18:32 +0000218 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000219 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000220 break;
221 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000222
223 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000224
Chris Lattner7ed31012006-02-01 01:29:47 +0000225 // ConstraintEnd may be either the next comma or the end of the string. In
226 // the former case, we skip the comma.
227 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000228 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000229 ++I;
230 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
231 }
232 }
233
234 return Result;
235}
236
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000237/// Verify - Verify that the specified constraint string is reasonable for the
238/// specified function type, and otherwise validate the constraint string.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000239bool InlineAsm::Verify(const FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000240 if (Ty->isVarArg()) return false;
241
Chris Lattner7ed31012006-02-01 01:29:47 +0000242 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000243
244 // Error parsing constraints.
245 if (Constraints.empty() && !ConstStr.empty()) return false;
246
247 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000248 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000249
250 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000251 switch (Constraints[i].Type) {
252 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000253 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
254 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000255 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000256 ++NumOutputs;
257 break;
258 }
Chris Lattneree00d042008-05-22 04:46:38 +0000259 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000260 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000261 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000262 if (NumClobbers) return false; // inputs before clobbers.
263 ++NumInputs;
264 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000265 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000266 ++NumClobbers;
267 break;
268 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000269 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000270
Chris Lattner10748d82008-04-27 23:33:55 +0000271 switch (NumOutputs) {
272 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000273 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000274 break;
275 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000276 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000277 break;
278 default:
279 const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
280 if (STy == 0 || STy->getNumElements() != NumOutputs)
281 return false;
282 break;
283 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000284
285 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000286 return true;
287}
Reid Spencer5113dc52006-06-07 23:03:13 +0000288