blob: 0520dfa17cedadf96ab4423b588ea2155501417e [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,
Dale Johannesenfd04c742009-10-13 20:46:56 +000030 const StringRef &Constraints, bool hasSideEffects,
31 bool isMsAsm) {
Chris Lattnera2d810d2006-01-25 22:26:05 +000032 // FIXME: memoize!
Dale Johannesenfd04c742009-10-13 20:46:56 +000033 return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects, isMsAsm);
Chris Lattnera2d810d2006-01-25 22:26:05 +000034}
35
Daniel Dunbard43b86d2009-07-25 06:02:13 +000036InlineAsm::InlineAsm(const FunctionType *Ty, const StringRef &asmString,
Dale Johannesenfd04c742009-10-13 20:46:56 +000037 const StringRef &constraints, bool hasSideEffects,
38 bool isMsAsm)
Christopher Lambedf07882007-12-17 01:12:55 +000039 : Value(PointerType::getUnqual(Ty),
40 Value::InlineAsmVal),
41 AsmString(asmString),
Dale Johannesenfd04c742009-10-13 20:46:56 +000042 Constraints(constraints), HasSideEffects(hasSideEffects), IsMsAsm(isMsAsm) {
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.
45 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
Chris Lattnereef2fe72006-01-24 04:13:11 +000046}
47
48const FunctionType *InlineAsm::getFunctionType() const {
49 return cast<FunctionType>(getType()->getElementType());
50}
Chris Lattnera2d810d2006-01-25 22:26:05 +000051
Chris Lattner7ed31012006-02-01 01:29:47 +000052/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
53/// fields in this structure. If the constraint string is not understood,
54/// return true, otherwise return false.
Daniel Dunbard43b86d2009-07-25 06:02:13 +000055bool InlineAsm::ConstraintInfo::Parse(const StringRef &Str,
Chris Lattner2f34a9e2006-02-02 00:23:53 +000056 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000057 StringRef::iterator I = Str.begin(), E = Str.end();
Chris Lattner7ed31012006-02-01 01:29:47 +000058
59 // Initialize
60 Type = isInput;
61 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000062 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000063 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000064 isIndirect = false;
Chris Lattner7ed31012006-02-01 01:29:47 +000065
Chris Lattnerc48e2c32007-04-28 01:02:58 +000066 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000067 if (*I == '~') {
68 Type = isClobber;
69 ++I;
70 } else if (*I == '=') {
71 ++I;
72 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000073 }
74
75 if (*I == '*') {
76 isIndirect = true;
77 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +000078 }
79
80 if (I == E) return true; // Just a prefix, like "==" or "~".
81
82 // Parse the modifiers.
83 bool DoneWithModifiers = false;
84 while (!DoneWithModifiers) {
85 switch (*I) {
86 default:
87 DoneWithModifiers = true;
88 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000089 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +000090 if (Type != isOutput || // Cannot early clobber anything but output.
91 isEarlyClobber) // Reject &&&&&&
92 return true;
93 isEarlyClobber = true;
94 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000095 case '%': // Commutative.
96 if (Type == isClobber || // Cannot commute clobbers.
97 isCommutative) // Reject %%%%%
98 return true;
99 isCommutative = true;
100 break;
101 case '#': // Comment.
102 case '*': // Register preferencing.
103 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000104 }
105
106 if (!DoneWithModifiers) {
107 ++I;
108 if (I == E) return true; // Just prefixes and modifiers!
109 }
110 }
111
112 // Parse the various constraints.
113 while (I != E) {
114 if (*I == '{') { // Physical register reference.
115 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000116 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000117 if (ConstraintEnd == E) return true; // "{foo"
118 Codes.push_back(std::string(I, ConstraintEnd+1));
119 I = ConstraintEnd+1;
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000120 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000121 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000122 StringRef::iterator NumStart = I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000123 while (I != E && isdigit(*I))
124 ++I;
125 Codes.push_back(std::string(NumStart, I));
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000126 unsigned N = atoi(Codes.back().c_str());
127 // Check that this is a valid matching constraint!
128 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
129 Type != isInput)
130 return true; // Invalid constraint number.
131
Chris Lattner860df6e2008-10-17 16:47:46 +0000132 // If Operand N already has a matching input, reject this. An output
133 // can't be constrained to the same value as multiple inputs.
134 if (ConstraintsSoFar[N].hasMatchingInput())
135 return true;
136
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000137 // Note that operand #n has a matching input.
Chris Lattner860df6e2008-10-17 16:47:46 +0000138 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
Chris Lattner7ed31012006-02-01 01:29:47 +0000139 } else {
140 // Single letter constraint.
141 Codes.push_back(std::string(I, I+1));
142 ++I;
143 }
144 }
145
146 return false;
147}
148
149std::vector<InlineAsm::ConstraintInfo>
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000150InlineAsm::ParseConstraints(const StringRef &Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000151 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000152
153 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000154 for (StringRef::iterator I = Constraints.begin(),
155 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000156 ConstraintInfo Info;
157
158 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000159 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000160
161 if (ConstraintEnd == I || // Empty constraint like ",,"
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000162 Info.Parse(std::string(I, ConstraintEnd), Result)) {
163 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000164 break;
165 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000166
167 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000168
Chris Lattner7ed31012006-02-01 01:29:47 +0000169 // ConstraintEnd may be either the next comma or the end of the string. In
170 // the former case, we skip the comma.
171 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000172 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000173 ++I;
174 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
175 }
176 }
177
178 return Result;
179}
180
181
182/// Verify - Verify that the specified constraint string is reasonable for the
183/// specified function type, and otherwise validate the constraint string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000184bool InlineAsm::Verify(const FunctionType *Ty, const StringRef &ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000185 if (Ty->isVarArg()) return false;
186
Chris Lattner7ed31012006-02-01 01:29:47 +0000187 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000188
189 // Error parsing constraints.
190 if (Constraints.empty() && !ConstStr.empty()) return false;
191
192 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000193 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000194
195 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000196 switch (Constraints[i].Type) {
197 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000198 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
199 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000200 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000201 ++NumOutputs;
202 break;
203 }
Chris Lattneree00d042008-05-22 04:46:38 +0000204 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000205 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000206 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000207 if (NumClobbers) return false; // inputs before clobbers.
208 ++NumInputs;
209 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000210 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000211 ++NumClobbers;
212 break;
213 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000214 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000215
Chris Lattner10748d82008-04-27 23:33:55 +0000216 switch (NumOutputs) {
217 case 0:
Owen Anderson55f1c092009-08-13 21:58:54 +0000218 if (Ty->getReturnType() != Type::getVoidTy(Ty->getContext())) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000219 break;
220 case 1:
221 if (isa<StructType>(Ty->getReturnType())) return false;
222 break;
223 default:
224 const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
225 if (STy == 0 || STy->getNumElements() != NumOutputs)
226 return false;
227 break;
228 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000229
230 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000231 return true;
232}
Reid Spencer5113dc52006-06-07 23:03:13 +0000233