blob: 6355834880bd26c1dda103e6548860af179b5169 [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"
15#include "llvm/DerivedTypes.h"
Jeff Cohenb24b66f2006-02-01 04:37:04 +000016#include <algorithm>
Chris Lattner8547e3a2006-01-26 00:48:33 +000017#include <cctype>
Chris Lattnereef2fe72006-01-24 04:13:11 +000018using namespace llvm;
19
Gordon Henriksen14a55692007-12-10 02:14:30 +000020// Implement the first virtual method in this class in this file so the
21// InlineAsm vtable is emitted here.
22InlineAsm::~InlineAsm() {
23}
24
25
Chris Lattnera2d810d2006-01-25 22:26:05 +000026// NOTE: when memoizing the function type, we have to be careful to handle the
27// case when the type gets refined.
28
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000029InlineAsm *InlineAsm::get(const FunctionType *Ty, StringRef AsmString,
30 StringRef Constraints, bool hasSideEffects,
Dale Johannesen1cfb9582009-10-21 23:28:00 +000031 bool isAlignStack) {
Chris Lattnera2d810d2006-01-25 22:26:05 +000032 // FIXME: memoize!
Dale Johannesen1cfb9582009-10-21 23:28:00 +000033 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects,
34 isAlignStack);
Chris Lattnera2d810d2006-01-25 22:26:05 +000035}
36
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000037InlineAsm::InlineAsm(const FunctionType *Ty, StringRef asmString,
38 StringRef constraints, bool hasSideEffects,
Dale Johannesen1cfb9582009-10-21 23:28:00 +000039 bool isAlignStack)
Christopher Lambedf07882007-12-17 01:12:55 +000040 : Value(PointerType::getUnqual(Ty),
41 Value::InlineAsmVal),
42 AsmString(asmString),
Dale Johannesen1cfb9582009-10-21 23:28:00 +000043 Constraints(constraints), HasSideEffects(hasSideEffects),
44 IsAlignStack(isAlignStack) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000045
Chris Lattnera2d810d2006-01-25 22:26:05 +000046 // Do various checks on the constraint string and type.
47 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
Chris Lattnereef2fe72006-01-24 04:13:11 +000048}
49
50const FunctionType *InlineAsm::getFunctionType() const {
51 return cast<FunctionType>(getType()->getElementType());
52}
Chris Lattnera2d810d2006-01-25 22:26:05 +000053
Chris Lattner7ed31012006-02-01 01:29:47 +000054/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
55/// fields in this structure. If the constraint string is not understood,
56/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000057bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
Chris Lattner2f34a9e2006-02-02 00:23:53 +000058 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000059 StringRef::iterator I = Str.begin(), E = Str.end();
Chris Lattner7ed31012006-02-01 01:29:47 +000060
61 // Initialize
62 Type = isInput;
63 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000064 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000065 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000066 isIndirect = false;
Chris Lattner7ed31012006-02-01 01:29:47 +000067
Chris Lattnerc48e2c32007-04-28 01:02:58 +000068 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000069 if (*I == '~') {
70 Type = isClobber;
71 ++I;
72 } else if (*I == '=') {
73 ++I;
74 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000075 }
76
77 if (*I == '*') {
78 isIndirect = true;
79 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +000080 }
81
82 if (I == E) return true; // Just a prefix, like "==" or "~".
83
84 // Parse the modifiers.
85 bool DoneWithModifiers = false;
86 while (!DoneWithModifiers) {
87 switch (*I) {
88 default:
89 DoneWithModifiers = true;
90 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000091 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +000092 if (Type != isOutput || // Cannot early clobber anything but output.
93 isEarlyClobber) // Reject &&&&&&
94 return true;
95 isEarlyClobber = true;
96 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000097 case '%': // Commutative.
98 if (Type == isClobber || // Cannot commute clobbers.
99 isCommutative) // Reject %%%%%
100 return true;
101 isCommutative = true;
102 break;
103 case '#': // Comment.
104 case '*': // Register preferencing.
105 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000106 }
107
108 if (!DoneWithModifiers) {
109 ++I;
110 if (I == E) return true; // Just prefixes and modifiers!
111 }
112 }
113
114 // Parse the various constraints.
115 while (I != E) {
116 if (*I == '{') { // Physical register reference.
117 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000118 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000119 if (ConstraintEnd == E) return true; // "{foo"
120 Codes.push_back(std::string(I, ConstraintEnd+1));
121 I = ConstraintEnd+1;
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000122 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000123 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000124 StringRef::iterator NumStart = I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000125 while (I != E && isdigit(*I))
126 ++I;
127 Codes.push_back(std::string(NumStart, I));
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000128 unsigned N = atoi(Codes.back().c_str());
129 // Check that this is a valid matching constraint!
130 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
131 Type != isInput)
132 return true; // Invalid constraint number.
133
Chris Lattner860df6e2008-10-17 16:47:46 +0000134 // If Operand N already has a matching input, reject this. An output
135 // can't be constrained to the same value as multiple inputs.
136 if (ConstraintsSoFar[N].hasMatchingInput())
137 return true;
138
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000139 // Note that operand #n has a matching input.
Chris Lattner860df6e2008-10-17 16:47:46 +0000140 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
Chris Lattner7ed31012006-02-01 01:29:47 +0000141 } else {
142 // Single letter constraint.
143 Codes.push_back(std::string(I, I+1));
144 ++I;
145 }
146 }
147
148 return false;
149}
150
151std::vector<InlineAsm::ConstraintInfo>
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000152InlineAsm::ParseConstraints(StringRef Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000153 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000154
155 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000156 for (StringRef::iterator I = Constraints.begin(),
157 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000158 ConstraintInfo Info;
159
160 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000161 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000162
163 if (ConstraintEnd == I || // Empty constraint like ",,"
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000164 Info.Parse(std::string(I, ConstraintEnd), Result)) {
165 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000166 break;
167 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000168
169 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000170
Chris Lattner7ed31012006-02-01 01:29:47 +0000171 // ConstraintEnd may be either the next comma or the end of the string. In
172 // the former case, we skip the comma.
173 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000174 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000175 ++I;
176 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
177 }
178 }
179
180 return Result;
181}
182
183
184/// Verify - Verify that the specified constraint string is reasonable for the
185/// specified function type, and otherwise validate the constraint string.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000186bool InlineAsm::Verify(const FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000187 if (Ty->isVarArg()) return false;
188
Chris Lattner7ed31012006-02-01 01:29:47 +0000189 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000190
191 // Error parsing constraints.
192 if (Constraints.empty() && !ConstStr.empty()) return false;
193
194 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000195 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000196
197 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000198 switch (Constraints[i].Type) {
199 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000200 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
201 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000202 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000203 ++NumOutputs;
204 break;
205 }
Chris Lattneree00d042008-05-22 04:46:38 +0000206 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000207 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000208 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000209 if (NumClobbers) return false; // inputs before clobbers.
210 ++NumInputs;
211 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000212 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000213 ++NumClobbers;
214 break;
215 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000216 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000217
Chris Lattner10748d82008-04-27 23:33:55 +0000218 switch (NumOutputs) {
219 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000220 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000221 break;
222 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000223 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000224 break;
225 default:
226 const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
227 if (STy == 0 || STy->getNumElements() != NumOutputs)
228 return false;
229 break;
230 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000231
232 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000233 return true;
234}
Reid Spencer5113dc52006-06-07 23:03:13 +0000235