blob: 485092e6ffb1e15275a5a6c4090a53956bd722cc [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 Dunbard43b86d2009-07-25 06:02:13 +000029InlineAsm *InlineAsm::get(const FunctionType *Ty, const StringRef &AsmString,
30 const StringRef &Constraints, bool hasSideEffects) {
Chris Lattnera2d810d2006-01-25 22:26:05 +000031 // FIXME: memoize!
32 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects);
33}
34
Daniel Dunbard43b86d2009-07-25 06:02:13 +000035InlineAsm::InlineAsm(const FunctionType *Ty, const StringRef &asmString,
36 const StringRef &constraints, bool hasSideEffects)
Christopher Lambedf07882007-12-17 01:12:55 +000037 : Value(PointerType::getUnqual(Ty),
38 Value::InlineAsmVal),
39 AsmString(asmString),
Chris Lattner8bbcda22006-01-25 18:57:27 +000040 Constraints(constraints), HasSideEffects(hasSideEffects) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000041
Chris Lattnera2d810d2006-01-25 22:26:05 +000042 // Do various checks on the constraint string and type.
43 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
Chris Lattnereef2fe72006-01-24 04:13:11 +000044}
45
46const FunctionType *InlineAsm::getFunctionType() const {
47 return cast<FunctionType>(getType()->getElementType());
48}
Chris Lattnera2d810d2006-01-25 22:26:05 +000049
Chris Lattner7ed31012006-02-01 01:29:47 +000050/// 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.
Daniel Dunbard43b86d2009-07-25 06:02:13 +000053bool InlineAsm::ConstraintInfo::Parse(const StringRef &Str,
Chris Lattner2f34a9e2006-02-02 00:23:53 +000054 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000055 StringRef::iterator I = Str.begin(), E = Str.end();
Chris Lattner7ed31012006-02-01 01:29:47 +000056
57 // Initialize
58 Type = isInput;
59 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000060 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000061 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000062 isIndirect = false;
Chris Lattner7ed31012006-02-01 01:29:47 +000063
Chris Lattnerc48e2c32007-04-28 01:02:58 +000064 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000065 if (*I == '~') {
66 Type = isClobber;
67 ++I;
68 } else if (*I == '=') {
69 ++I;
70 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000071 }
72
73 if (*I == '*') {
74 isIndirect = true;
75 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +000076 }
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;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000087 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +000088 if (Type != isOutput || // Cannot early clobber anything but output.
89 isEarlyClobber) // Reject &&&&&&
90 return true;
91 isEarlyClobber = true;
92 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000093 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.
Chris Lattner7ed31012006-02-01 01:29:47 +0000102 }
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.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000114 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000115 if (ConstraintEnd == E) return true; // "{foo"
116 Codes.push_back(std::string(I, ConstraintEnd+1));
117 I = ConstraintEnd+1;
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000118 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000119 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000120 StringRef::iterator NumStart = I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000121 while (I != E && isdigit(*I))
122 ++I;
123 Codes.push_back(std::string(NumStart, I));
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000124 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
Chris Lattner860df6e2008-10-17 16:47:46 +0000130 // If Operand N already has a matching input, reject this. An output
131 // can't be constrained to the same value as multiple inputs.
132 if (ConstraintsSoFar[N].hasMatchingInput())
133 return true;
134
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000135 // Note that operand #n has a matching input.
Chris Lattner860df6e2008-10-17 16:47:46 +0000136 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
Chris Lattner7ed31012006-02-01 01:29:47 +0000137 } else {
138 // Single letter constraint.
139 Codes.push_back(std::string(I, I+1));
140 ++I;
141 }
142 }
143
144 return false;
145}
146
147std::vector<InlineAsm::ConstraintInfo>
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000148InlineAsm::ParseConstraints(const StringRef &Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000149 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000150
151 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000152 for (StringRef::iterator I = Constraints.begin(),
153 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000154 ConstraintInfo Info;
155
156 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000157 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000158
159 if (ConstraintEnd == I || // Empty constraint like ",,"
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000160 Info.Parse(std::string(I, ConstraintEnd), Result)) {
161 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000162 break;
163 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000164
165 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000166
Chris Lattner7ed31012006-02-01 01:29:47 +0000167 // ConstraintEnd may be either the next comma or the end of the string. In
168 // the former case, we skip the comma.
169 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000170 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000171 ++I;
172 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
173 }
174 }
175
176 return Result;
177}
178
179
180/// Verify - Verify that the specified constraint string is reasonable for the
181/// specified function type, and otherwise validate the constraint string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000182bool InlineAsm::Verify(const FunctionType *Ty, const StringRef &ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000183 if (Ty->isVarArg()) return false;
184
Chris Lattner7ed31012006-02-01 01:29:47 +0000185 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000186
187 // Error parsing constraints.
188 if (Constraints.empty() && !ConstStr.empty()) return false;
189
190 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000191 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000192
193 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000194 switch (Constraints[i].Type) {
195 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000196 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
197 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000198 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000199 ++NumOutputs;
200 break;
201 }
Chris Lattneree00d042008-05-22 04:46:38 +0000202 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000203 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000204 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000205 if (NumClobbers) return false; // inputs before clobbers.
206 ++NumInputs;
207 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000208 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000209 ++NumClobbers;
210 break;
211 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000212 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000213
Chris Lattner10748d82008-04-27 23:33:55 +0000214 switch (NumOutputs) {
215 case 0:
216 if (Ty->getReturnType() != Type::VoidTy) return false;
217 break;
218 case 1:
219 if (isa<StructType>(Ty->getReturnType())) return false;
220 break;
221 default:
222 const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
223 if (STy == 0 || STy->getNumElements() != NumOutputs)
224 return false;
225 break;
226 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000227
228 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000229 return true;
230}
Reid Spencer5113dc52006-06-07 23:03:13 +0000231