2006-01-24 04:13:11 +00:00
|
|
|
//===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-01-24 04:13:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the InlineAsm class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/InlineAsm.h"
|
2010-03-21 20:37:19 +00:00
|
|
|
#include "ConstantsContext.h"
|
|
|
|
#include "LLVMContextImpl.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2006-02-01 04:37:04 +00:00
|
|
|
#include <algorithm>
|
2006-01-26 00:48:33 +00:00
|
|
|
#include <cctype>
|
2006-01-24 04:13:11 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2007-12-10 02:14:30 +00:00
|
|
|
// Implement the first virtual method in this class in this file so the
|
|
|
|
// InlineAsm vtable is emitted here.
|
|
|
|
InlineAsm::~InlineAsm() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
InlineAsm *InlineAsm::get(FunctionType *Ty, StringRef AsmString,
|
2009-11-06 10:58:06 +00:00
|
|
|
StringRef Constraints, bool hasSideEffects,
|
2012-09-05 19:00:49 +00:00
|
|
|
bool isAlignStack, AsmDialect asmDialect) {
|
2012-09-04 22:46:24 +00:00
|
|
|
InlineAsmKeyType Key(AsmString, Constraints, hasSideEffects, isAlignStack,
|
|
|
|
asmDialect);
|
2010-03-21 20:37:19 +00:00
|
|
|
LLVMContextImpl *pImpl = Ty->getContext().pImpl;
|
|
|
|
return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(Ty), Key);
|
2006-01-25 22:26:05 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
InlineAsm::InlineAsm(PointerType *Ty, const std::string &asmString,
|
2010-03-21 20:37:19 +00:00
|
|
|
const std::string &constraints, bool hasSideEffects,
|
2012-09-05 19:00:49 +00:00
|
|
|
bool isAlignStack, AsmDialect asmDialect)
|
2010-03-21 20:37:19 +00:00
|
|
|
: Value(Ty, Value::InlineAsmVal),
|
2012-09-04 22:46:24 +00:00
|
|
|
AsmString(asmString), Constraints(constraints),
|
|
|
|
HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
|
2012-09-05 19:00:49 +00:00
|
|
|
Dialect(asmDialect) {
|
2006-01-24 04:13:11 +00:00
|
|
|
|
2006-01-25 22:26:05 +00:00
|
|
|
// Do various checks on the constraint string and type.
|
2010-03-21 20:37:19 +00:00
|
|
|
assert(Verify(getFunctionType(), constraints) &&
|
|
|
|
"Function type not legal for constraints!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void InlineAsm::destroyConstant() {
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
getType()->getContext().pImpl->InlineAsms.remove(this);
|
2010-03-21 20:37:19 +00:00
|
|
|
delete this;
|
2006-01-24 04:13:11 +00:00
|
|
|
}
|
|
|
|
|
2011-07-15 23:15:45 +00:00
|
|
|
FunctionType *InlineAsm::getFunctionType() const {
|
2006-01-24 04:13:11 +00:00
|
|
|
return cast<FunctionType>(getType()->getElementType());
|
|
|
|
}
|
2010-09-13 18:15:37 +00:00
|
|
|
|
|
|
|
///Default constructor.
|
|
|
|
InlineAsm::ConstraintInfo::ConstraintInfo() :
|
|
|
|
Type(isInput), isEarlyClobber(false),
|
|
|
|
MatchingInput(-1), isCommutative(false),
|
2010-09-13 18:25:05 +00:00
|
|
|
isIndirect(false), isMultipleAlternative(false),
|
|
|
|
currentAlternativeIndex(0) {
|
2010-09-13 18:15:37 +00:00
|
|
|
}
|
|
|
|
|
2006-02-01 01:29:47 +00:00
|
|
|
/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
|
|
|
|
/// fields in this structure. If the constraint string is not understood,
|
|
|
|
/// return true, otherwise return false.
|
2009-11-06 10:58:06 +00:00
|
|
|
bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
|
2010-10-29 17:29:13 +00:00
|
|
|
InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
|
2009-07-25 06:02:13 +00:00
|
|
|
StringRef::iterator I = Str.begin(), E = Str.end();
|
2010-09-13 18:15:37 +00:00
|
|
|
unsigned multipleAlternativeCount = Str.count('|') + 1;
|
|
|
|
unsigned multipleAlternativeIndex = 0;
|
2010-10-29 17:29:13 +00:00
|
|
|
ConstraintCodeVector *pCodes = &Codes;
|
2015-02-10 21:15:06 +00:00
|
|
|
|
2006-02-01 01:29:47 +00:00
|
|
|
// Initialize
|
2015-03-09 01:57:13 +00:00
|
|
|
isMultipleAlternative = multipleAlternativeCount > 1;
|
2010-09-13 18:15:37 +00:00
|
|
|
if (isMultipleAlternative) {
|
|
|
|
multipleAlternatives.resize(multipleAlternativeCount);
|
|
|
|
pCodes = &multipleAlternatives[0].Codes;
|
|
|
|
}
|
2006-02-01 01:29:47 +00:00
|
|
|
Type = isInput;
|
|
|
|
isEarlyClobber = false;
|
2008-10-17 16:47:46 +00:00
|
|
|
MatchingInput = -1;
|
2006-02-23 23:36:53 +00:00
|
|
|
isCommutative = false;
|
2007-04-28 01:02:58 +00:00
|
|
|
isIndirect = false;
|
2010-09-13 18:15:37 +00:00
|
|
|
currentAlternativeIndex = 0;
|
2006-02-01 01:29:47 +00:00
|
|
|
|
2007-04-28 01:02:58 +00:00
|
|
|
// Parse prefixes.
|
2006-02-01 01:29:47 +00:00
|
|
|
if (*I == '~') {
|
|
|
|
Type = isClobber;
|
|
|
|
++I;
|
2014-09-05 22:30:32 +00:00
|
|
|
|
|
|
|
// '{' must immediately follow '~'.
|
|
|
|
if (I != E && *I != '{')
|
|
|
|
return true;
|
2006-02-01 01:29:47 +00:00
|
|
|
} else if (*I == '=') {
|
|
|
|
++I;
|
|
|
|
Type = isOutput;
|
2007-04-28 01:02:58 +00:00
|
|
|
}
|
2015-02-10 21:15:06 +00:00
|
|
|
|
2007-04-28 01:02:58 +00:00
|
|
|
if (*I == '*') {
|
|
|
|
isIndirect = true;
|
|
|
|
++I;
|
2006-02-01 01:29:47 +00:00
|
|
|
}
|
2015-02-10 21:15:06 +00:00
|
|
|
|
2006-02-01 01:29:47 +00:00
|
|
|
if (I == E) return true; // Just a prefix, like "==" or "~".
|
|
|
|
|
|
|
|
// Parse the modifiers.
|
|
|
|
bool DoneWithModifiers = false;
|
|
|
|
while (!DoneWithModifiers) {
|
|
|
|
switch (*I) {
|
|
|
|
default:
|
|
|
|
DoneWithModifiers = true;
|
|
|
|
break;
|
2006-02-23 23:36:53 +00:00
|
|
|
case '&': // Early clobber.
|
2006-02-01 01:29:47 +00:00
|
|
|
if (Type != isOutput || // Cannot early clobber anything but output.
|
|
|
|
isEarlyClobber) // Reject &&&&&&
|
|
|
|
return true;
|
|
|
|
isEarlyClobber = true;
|
|
|
|
break;
|
2006-02-23 23:36:53 +00:00
|
|
|
case '%': // Commutative.
|
|
|
|
if (Type == isClobber || // Cannot commute clobbers.
|
|
|
|
isCommutative) // Reject %%%%%
|
|
|
|
return true;
|
|
|
|
isCommutative = true;
|
|
|
|
break;
|
|
|
|
case '#': // Comment.
|
|
|
|
case '*': // Register preferencing.
|
|
|
|
return true; // Not supported.
|
2006-02-01 01:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoneWithModifiers) {
|
2006-01-26 00:48:33 +00:00
|
|
|
++I;
|
2006-02-01 01:29:47 +00:00
|
|
|
if (I == E) return true; // Just prefixes and modifiers!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the various constraints.
|
|
|
|
while (I != E) {
|
|
|
|
if (*I == '{') { // Physical register reference.
|
|
|
|
// Find the end of the register name.
|
2009-07-25 06:02:13 +00:00
|
|
|
StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
|
2006-02-01 01:29:47 +00:00
|
|
|
if (ConstraintEnd == E) return true; // "{foo"
|
2010-09-13 18:15:37 +00:00
|
|
|
pCodes->push_back(std::string(I, ConstraintEnd+1));
|
2006-02-01 01:29:47 +00:00
|
|
|
I = ConstraintEnd+1;
|
2013-02-12 21:21:59 +00:00
|
|
|
} else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
|
2006-02-01 01:29:47 +00:00
|
|
|
// Maximal munch numbers.
|
2009-07-25 06:02:13 +00:00
|
|
|
StringRef::iterator NumStart = I;
|
2013-02-12 21:21:59 +00:00
|
|
|
while (I != E && isdigit(static_cast<unsigned char>(*I)))
|
2006-01-26 00:48:33 +00:00
|
|
|
++I;
|
2010-09-13 18:15:37 +00:00
|
|
|
pCodes->push_back(std::string(NumStart, I));
|
|
|
|
unsigned N = atoi(pCodes->back().c_str());
|
2006-02-02 00:23:53 +00:00
|
|
|
// Check that this is a valid matching constraint!
|
|
|
|
if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
|
|
|
|
Type != isInput)
|
|
|
|
return true; // Invalid constraint number.
|
|
|
|
|
2008-10-17 16:47:46 +00:00
|
|
|
// If Operand N already has a matching input, reject this. An output
|
|
|
|
// can't be constrained to the same value as multiple inputs.
|
2010-09-13 18:15:37 +00:00
|
|
|
if (isMultipleAlternative) {
|
|
|
|
InlineAsm::SubConstraintInfo &scInfo =
|
|
|
|
ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
|
|
|
|
if (scInfo.MatchingInput != -1)
|
|
|
|
return true;
|
|
|
|
// Note that operand #n has a matching input.
|
|
|
|
scInfo.MatchingInput = ConstraintsSoFar.size();
|
|
|
|
} else {
|
2015-03-29 20:33:07 +00:00
|
|
|
if (ConstraintsSoFar[N].hasMatchingInput() &&
|
|
|
|
ConstraintsSoFar[N].MatchingInput != ConstraintsSoFar.size())
|
2010-09-13 18:15:37 +00:00
|
|
|
return true;
|
|
|
|
// Note that operand #n has a matching input.
|
|
|
|
ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
|
|
|
|
}
|
|
|
|
} else if (*I == '|') {
|
|
|
|
multipleAlternativeIndex++;
|
|
|
|
pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
|
|
|
|
++I;
|
2011-06-02 19:26:37 +00:00
|
|
|
} else if (*I == '^') {
|
|
|
|
// Multi-letter constraint
|
2011-06-03 22:09:12 +00:00
|
|
|
// FIXME: For now assuming these are 2-character constraints.
|
|
|
|
pCodes->push_back(std::string(I+1, I+3));
|
|
|
|
I += 3;
|
2006-02-01 01:29:47 +00:00
|
|
|
} else {
|
|
|
|
// Single letter constraint.
|
2010-09-13 18:15:37 +00:00
|
|
|
pCodes->push_back(std::string(I, I+1));
|
2006-02-01 01:29:47 +00:00
|
|
|
++I;
|
2006-01-26 00:48:33 +00:00
|
|
|
}
|
2006-02-01 01:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-13 18:15:37 +00:00
|
|
|
/// selectAlternative - Point this constraint to the alternative constraint
|
|
|
|
/// indicated by the index.
|
|
|
|
void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
|
|
|
|
if (index < multipleAlternatives.size()) {
|
|
|
|
currentAlternativeIndex = index;
|
|
|
|
InlineAsm::SubConstraintInfo &scInfo =
|
|
|
|
multipleAlternatives[currentAlternativeIndex];
|
|
|
|
MatchingInput = scInfo.MatchingInput;
|
|
|
|
Codes = scInfo.Codes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-29 17:29:13 +00:00
|
|
|
InlineAsm::ConstraintInfoVector
|
2009-11-06 10:58:06 +00:00
|
|
|
InlineAsm::ParseConstraints(StringRef Constraints) {
|
2010-10-29 17:29:13 +00:00
|
|
|
ConstraintInfoVector Result;
|
2006-02-01 01:29:47 +00:00
|
|
|
|
|
|
|
// Scan the constraints string.
|
2009-07-25 06:02:13 +00:00
|
|
|
for (StringRef::iterator I = Constraints.begin(),
|
|
|
|
E = Constraints.end(); I != E; ) {
|
2006-02-01 01:29:47 +00:00
|
|
|
ConstraintInfo Info;
|
|
|
|
|
|
|
|
// Find the end of this constraint.
|
2009-07-25 06:02:13 +00:00
|
|
|
StringRef::iterator ConstraintEnd = std::find(I, E, ',');
|
2006-02-01 01:29:47 +00:00
|
|
|
|
|
|
|
if (ConstraintEnd == I || // Empty constraint like ",,"
|
2010-07-25 23:18:32 +00:00
|
|
|
Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
|
2006-02-02 00:23:53 +00:00
|
|
|
Result.clear(); // Erroneous constraint?
|
2006-01-26 02:21:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-02-01 01:29:47 +00:00
|
|
|
|
|
|
|
Result.push_back(Info);
|
2006-01-26 00:48:33 +00:00
|
|
|
|
2006-02-01 01:29:47 +00:00
|
|
|
// ConstraintEnd may be either the next comma or the end of the string. In
|
|
|
|
// the former case, we skip the comma.
|
|
|
|
I = ConstraintEnd;
|
2006-01-26 02:21:59 +00:00
|
|
|
if (I != E) {
|
|
|
|
++I;
|
2015-02-10 21:15:06 +00:00
|
|
|
if (I == E) {
|
|
|
|
Result.clear();
|
|
|
|
break;
|
|
|
|
} // don't allow "xyz,"
|
2006-01-26 02:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify - Verify that the specified constraint string is reasonable for the
|
|
|
|
/// specified function type, and otherwise validate the constraint string.
|
2011-07-18 04:54:35 +00:00
|
|
|
bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
|
2006-01-26 02:21:59 +00:00
|
|
|
if (Ty->isVarArg()) return false;
|
|
|
|
|
2010-10-29 17:29:13 +00:00
|
|
|
ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
|
2006-01-26 02:21:59 +00:00
|
|
|
|
|
|
|
// Error parsing constraints.
|
|
|
|
if (Constraints.empty() && !ConstStr.empty()) return false;
|
|
|
|
|
|
|
|
unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
|
2008-05-22 04:46:38 +00:00
|
|
|
unsigned NumIndirect = 0;
|
2006-01-26 02:21:59 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
|
2006-02-01 01:29:47 +00:00
|
|
|
switch (Constraints[i].Type) {
|
|
|
|
case InlineAsm::isOutput:
|
2008-05-22 04:46:38 +00:00
|
|
|
if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
|
|
|
|
return false; // outputs before inputs and clobbers.
|
2007-04-28 01:02:58 +00:00
|
|
|
if (!Constraints[i].isIndirect) {
|
2006-02-01 01:29:47 +00:00
|
|
|
++NumOutputs;
|
|
|
|
break;
|
|
|
|
}
|
2008-05-22 04:46:38 +00:00
|
|
|
++NumIndirect;
|
2007-04-28 01:02:58 +00:00
|
|
|
// FALLTHROUGH for Indirect Outputs.
|
2006-02-01 01:29:47 +00:00
|
|
|
case InlineAsm::isInput:
|
2006-01-26 00:48:33 +00:00
|
|
|
if (NumClobbers) return false; // inputs before clobbers.
|
|
|
|
++NumInputs;
|
|
|
|
break;
|
2006-02-01 01:29:47 +00:00
|
|
|
case InlineAsm::isClobber:
|
2006-01-26 00:48:33 +00:00
|
|
|
++NumClobbers;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-27 23:33:55 +00:00
|
|
|
switch (NumOutputs) {
|
|
|
|
case 0:
|
2010-01-05 13:12:22 +00:00
|
|
|
if (!Ty->getReturnType()->isVoidTy()) return false;
|
2008-04-27 23:33:55 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2010-02-16 11:11:14 +00:00
|
|
|
if (Ty->getReturnType()->isStructTy()) return false;
|
2008-04-27 23:33:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
|
2014-04-09 06:08:46 +00:00
|
|
|
if (!STy || STy->getNumElements() != NumOutputs)
|
2008-04-27 23:33:55 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2006-01-26 00:48:33 +00:00
|
|
|
|
|
|
|
if (Ty->getNumParams() != NumInputs) return false;
|
2006-01-25 22:26:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-06-07 23:03:13 +00:00
|
|
|
|