blob: 69f713b2c42c29d3d1f10cceea2a698784c5c347 [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}
Chris Lattnera2d810d2006-01-25 22:26:05 +000056
Chris Lattner7ed31012006-02-01 01:29:47 +000057/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
58/// fields in this structure. If the constraint string is not understood,
59/// return true, otherwise return false.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000060bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
Chris Lattner2f34a9e2006-02-02 00:23:53 +000061 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
Daniel Dunbard43b86d2009-07-25 06:02:13 +000062 StringRef::iterator I = Str.begin(), E = Str.end();
Chris Lattner7ed31012006-02-01 01:29:47 +000063
64 // Initialize
65 Type = isInput;
66 isEarlyClobber = false;
Chris Lattner860df6e2008-10-17 16:47:46 +000067 MatchingInput = -1;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000068 isCommutative = false;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000069 isIndirect = false;
Chris Lattner7ed31012006-02-01 01:29:47 +000070
Chris Lattnerc48e2c32007-04-28 01:02:58 +000071 // Parse prefixes.
Chris Lattner7ed31012006-02-01 01:29:47 +000072 if (*I == '~') {
73 Type = isClobber;
74 ++I;
75 } else if (*I == '=') {
76 ++I;
77 Type = isOutput;
Chris Lattnerc48e2c32007-04-28 01:02:58 +000078 }
79
80 if (*I == '*') {
81 isIndirect = true;
82 ++I;
Chris Lattner7ed31012006-02-01 01:29:47 +000083 }
84
85 if (I == E) return true; // Just a prefix, like "==" or "~".
86
87 // Parse the modifiers.
88 bool DoneWithModifiers = false;
89 while (!DoneWithModifiers) {
90 switch (*I) {
91 default:
92 DoneWithModifiers = true;
93 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +000094 case '&': // Early clobber.
Chris Lattner7ed31012006-02-01 01:29:47 +000095 if (Type != isOutput || // Cannot early clobber anything but output.
96 isEarlyClobber) // Reject &&&&&&
97 return true;
98 isEarlyClobber = true;
99 break;
Chris Lattnerde5a9f42006-02-23 23:36:53 +0000100 case '%': // Commutative.
101 if (Type == isClobber || // Cannot commute clobbers.
102 isCommutative) // Reject %%%%%
103 return true;
104 isCommutative = true;
105 break;
106 case '#': // Comment.
107 case '*': // Register preferencing.
108 return true; // Not supported.
Chris Lattner7ed31012006-02-01 01:29:47 +0000109 }
110
111 if (!DoneWithModifiers) {
112 ++I;
113 if (I == E) return true; // Just prefixes and modifiers!
114 }
115 }
116
117 // Parse the various constraints.
118 while (I != E) {
119 if (*I == '{') { // Physical register reference.
120 // Find the end of the register name.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000121 StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
Chris Lattner7ed31012006-02-01 01:29:47 +0000122 if (ConstraintEnd == E) return true; // "{foo"
123 Codes.push_back(std::string(I, ConstraintEnd+1));
124 I = ConstraintEnd+1;
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000125 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000126 // Maximal munch numbers.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000127 StringRef::iterator NumStart = I;
Chris Lattner7ed31012006-02-01 01:29:47 +0000128 while (I != E && isdigit(*I))
129 ++I;
130 Codes.push_back(std::string(NumStart, I));
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000131 unsigned N = atoi(Codes.back().c_str());
132 // Check that this is a valid matching constraint!
133 if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
134 Type != isInput)
135 return true; // Invalid constraint number.
136
Chris Lattner860df6e2008-10-17 16:47:46 +0000137 // If Operand N already has a matching input, reject this. An output
138 // can't be constrained to the same value as multiple inputs.
139 if (ConstraintsSoFar[N].hasMatchingInput())
140 return true;
141
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000142 // Note that operand #n has a matching input.
Chris Lattner860df6e2008-10-17 16:47:46 +0000143 ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
Chris Lattner7ed31012006-02-01 01:29:47 +0000144 } else {
145 // Single letter constraint.
146 Codes.push_back(std::string(I, I+1));
147 ++I;
148 }
149 }
150
151 return false;
152}
153
154std::vector<InlineAsm::ConstraintInfo>
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000155InlineAsm::ParseConstraints(StringRef Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000156 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000157
158 // Scan the constraints string.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000159 for (StringRef::iterator I = Constraints.begin(),
160 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000161 ConstraintInfo Info;
162
163 // Find the end of this constraint.
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000164 StringRef::iterator ConstraintEnd = std::find(I, E, ',');
Chris Lattner7ed31012006-02-01 01:29:47 +0000165
166 if (ConstraintEnd == I || // Empty constraint like ",,"
Benjamin Kramer5aaf6772010-07-25 23:18:32 +0000167 Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
Chris Lattner2f34a9e2006-02-02 00:23:53 +0000168 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000169 break;
170 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000171
172 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000173
Chris Lattner7ed31012006-02-01 01:29:47 +0000174 // ConstraintEnd may be either the next comma or the end of the string. In
175 // the former case, we skip the comma.
176 I = ConstraintEnd;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000177 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000178 ++I;
179 if (I == E) { Result.clear(); break; } // don't allow "xyz,"
180 }
181 }
182
183 return Result;
184}
185
186
187/// Verify - Verify that the specified constraint string is reasonable for the
188/// specified function type, and otherwise validate the constraint string.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000189bool InlineAsm::Verify(const FunctionType *Ty, StringRef ConstStr) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000190 if (Ty->isVarArg()) return false;
191
Chris Lattner7ed31012006-02-01 01:29:47 +0000192 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000193
194 // Error parsing constraints.
195 if (Constraints.empty() && !ConstStr.empty()) return false;
196
197 unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
Chris Lattneree00d042008-05-22 04:46:38 +0000198 unsigned NumIndirect = 0;
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000199
200 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000201 switch (Constraints[i].Type) {
202 case InlineAsm::isOutput:
Chris Lattneree00d042008-05-22 04:46:38 +0000203 if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
204 return false; // outputs before inputs and clobbers.
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000205 if (!Constraints[i].isIndirect) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000206 ++NumOutputs;
207 break;
208 }
Chris Lattneree00d042008-05-22 04:46:38 +0000209 ++NumIndirect;
Chris Lattnerc48e2c32007-04-28 01:02:58 +0000210 // FALLTHROUGH for Indirect Outputs.
Chris Lattner7ed31012006-02-01 01:29:47 +0000211 case InlineAsm::isInput:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000212 if (NumClobbers) return false; // inputs before clobbers.
213 ++NumInputs;
214 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000215 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000216 ++NumClobbers;
217 break;
218 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000219 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000220
Chris Lattner10748d82008-04-27 23:33:55 +0000221 switch (NumOutputs) {
222 case 0:
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000223 if (!Ty->getReturnType()->isVoidTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000224 break;
225 case 1:
Duncan Sands19d0b472010-02-16 11:11:14 +0000226 if (Ty->getReturnType()->isStructTy()) return false;
Chris Lattner10748d82008-04-27 23:33:55 +0000227 break;
228 default:
229 const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
230 if (STy == 0 || STy->getNumElements() != NumOutputs)
231 return false;
232 break;
233 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000234
235 if (Ty->getNumParams() != NumInputs) return false;
Chris Lattnera2d810d2006-01-25 22:26:05 +0000236 return true;
237}
Reid Spencer5113dc52006-06-07 23:03:13 +0000238