2017-02-17 08:00:09 +08:00
|
|
|
//===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//
|
2006-01-24 12:13:11 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-01-24 12:13:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the InlineAsm class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/IR/InlineAsm.h"
|
2010-03-22 04:37:19 +08:00
|
|
|
#include "ConstantsContext.h"
|
|
|
|
#include "LLVMContextImpl.h"
|
2017-02-17 08:00:09 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2017-02-17 08:00:09 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-02-01 12:37:04 +08:00
|
|
|
#include <algorithm>
|
2017-02-17 08:00:09 +08:00
|
|
|
#include <cassert>
|
2006-01-26 08:48:33 +08:00
|
|
|
#include <cctype>
|
2017-02-17 08:00:09 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2007-12-10 10:14:30 +08:00
|
|
|
|
2017-02-17 08:00:09 +08:00
|
|
|
using namespace llvm;
|
2006-01-26 06:26:05 +08:00
|
|
|
|
2015-07-28 08:06:38 +08:00
|
|
|
InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
|
2010-03-22 04:37:19 +08:00
|
|
|
const std::string &constraints, bool hasSideEffects,
|
2012-09-06 03:00:49 +08:00
|
|
|
bool isAlignStack, AsmDialect asmDialect)
|
2015-07-28 08:06:38 +08:00
|
|
|
: Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),
|
|
|
|
AsmString(asmString), Constraints(constraints), FTy(FTy),
|
|
|
|
HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
|
|
|
|
Dialect(asmDialect) {
|
2006-01-26 06:26:05 +08:00
|
|
|
// Do various checks on the constraint string and type.
|
2010-03-22 04:37:19 +08:00
|
|
|
assert(Verify(getFunctionType(), constraints) &&
|
|
|
|
"Function type not legal for constraints!");
|
|
|
|
}
|
|
|
|
|
2017-02-17 08:00:09 +08:00
|
|
|
InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,
|
|
|
|
StringRef Constraints, bool hasSideEffects,
|
|
|
|
bool isAlignStack, AsmDialect asmDialect) {
|
|
|
|
InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,
|
|
|
|
isAlignStack, asmDialect);
|
|
|
|
LLVMContextImpl *pImpl = FTy->getContext().pImpl;
|
|
|
|
return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);
|
|
|
|
}
|
|
|
|
|
2010-03-22 04:37:19 +08:00
|
|
|
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.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
getType()->getContext().pImpl->InlineAsms.remove(this);
|
2010-03-22 04:37:19 +08:00
|
|
|
delete this;
|
2006-01-24 12:13:11 +08:00
|
|
|
}
|
|
|
|
|
2011-07-16 07:15:45 +08:00
|
|
|
FunctionType *InlineAsm::getFunctionType() const {
|
2015-07-28 08:06:38 +08:00
|
|
|
return FTy;
|
2006-01-24 12:13:11 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-02-01 09:29:47 +08: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 18:58:06 +08:00
|
|
|
bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
|
2010-10-30 01:29:13 +08:00
|
|
|
InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
|
2009-07-25 14:02:13 +08:00
|
|
|
StringRef::iterator I = Str.begin(), E = Str.end();
|
2010-09-14 02:15:37 +08:00
|
|
|
unsigned multipleAlternativeCount = Str.count('|') + 1;
|
|
|
|
unsigned multipleAlternativeIndex = 0;
|
2010-10-30 01:29:13 +08:00
|
|
|
ConstraintCodeVector *pCodes = &Codes;
|
2015-02-11 05:15:06 +08:00
|
|
|
|
2006-02-01 09:29:47 +08:00
|
|
|
// Initialize
|
2015-03-09 09:57:13 +08:00
|
|
|
isMultipleAlternative = multipleAlternativeCount > 1;
|
2010-09-14 02:15:37 +08:00
|
|
|
if (isMultipleAlternative) {
|
|
|
|
multipleAlternatives.resize(multipleAlternativeCount);
|
|
|
|
pCodes = &multipleAlternatives[0].Codes;
|
|
|
|
}
|
2006-02-01 09:29:47 +08:00
|
|
|
Type = isInput;
|
|
|
|
isEarlyClobber = false;
|
2008-10-18 00:47:46 +08:00
|
|
|
MatchingInput = -1;
|
2006-02-24 07:36:53 +08:00
|
|
|
isCommutative = false;
|
2007-04-28 09:02:58 +08:00
|
|
|
isIndirect = false;
|
2010-09-14 02:15:37 +08:00
|
|
|
currentAlternativeIndex = 0;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2007-04-28 09:02:58 +08:00
|
|
|
// Parse prefixes.
|
2006-02-01 09:29:47 +08:00
|
|
|
if (*I == '~') {
|
|
|
|
Type = isClobber;
|
|
|
|
++I;
|
2014-09-06 06:30:32 +08:00
|
|
|
|
|
|
|
// '{' must immediately follow '~'.
|
|
|
|
if (I != E && *I != '{')
|
|
|
|
return true;
|
2006-02-01 09:29:47 +08:00
|
|
|
} else if (*I == '=') {
|
|
|
|
++I;
|
|
|
|
Type = isOutput;
|
2007-04-28 09:02:58 +08:00
|
|
|
}
|
2015-02-11 05:15:06 +08:00
|
|
|
|
2007-04-28 09:02:58 +08:00
|
|
|
if (*I == '*') {
|
|
|
|
isIndirect = true;
|
|
|
|
++I;
|
2006-02-01 09:29:47 +08:00
|
|
|
}
|
2015-02-11 05:15:06 +08:00
|
|
|
|
2006-02-01 09:29:47 +08:00
|
|
|
if (I == E) return true; // Just a prefix, like "==" or "~".
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-02-01 09:29:47 +08:00
|
|
|
// Parse the modifiers.
|
|
|
|
bool DoneWithModifiers = false;
|
|
|
|
while (!DoneWithModifiers) {
|
|
|
|
switch (*I) {
|
|
|
|
default:
|
|
|
|
DoneWithModifiers = true;
|
|
|
|
break;
|
2006-02-24 07:36:53 +08:00
|
|
|
case '&': // Early clobber.
|
2006-02-01 09:29:47 +08:00
|
|
|
if (Type != isOutput || // Cannot early clobber anything but output.
|
|
|
|
isEarlyClobber) // Reject &&&&&&
|
|
|
|
return true;
|
|
|
|
isEarlyClobber = true;
|
|
|
|
break;
|
2006-02-24 07:36:53 +08: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 09:29:47 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-02-01 09:29:47 +08:00
|
|
|
if (!DoneWithModifiers) {
|
2006-01-26 08:48:33 +08:00
|
|
|
++I;
|
2006-02-01 09:29:47 +08:00
|
|
|
if (I == E) return true; // Just prefixes and modifiers!
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-02-01 09:29:47 +08:00
|
|
|
// Parse the various constraints.
|
|
|
|
while (I != E) {
|
|
|
|
if (*I == '{') { // Physical register reference.
|
|
|
|
// Find the end of the register name.
|
2009-07-25 14:02:13 +08:00
|
|
|
StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
|
2006-02-01 09:29:47 +08:00
|
|
|
if (ConstraintEnd == E) return true; // "{foo"
|
2016-05-23 21:58:04 +08:00
|
|
|
pCodes->push_back(StringRef(I, ConstraintEnd+1 - I));
|
2006-02-01 09:29:47 +08:00
|
|
|
I = ConstraintEnd+1;
|
2013-02-13 05:21:59 +08:00
|
|
|
} else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
|
2006-02-01 09:29:47 +08:00
|
|
|
// Maximal munch numbers.
|
2009-07-25 14:02:13 +08:00
|
|
|
StringRef::iterator NumStart = I;
|
2013-02-13 05:21:59 +08:00
|
|
|
while (I != E && isdigit(static_cast<unsigned char>(*I)))
|
2006-01-26 08:48:33 +08:00
|
|
|
++I;
|
2016-05-23 21:58:04 +08:00
|
|
|
pCodes->push_back(StringRef(NumStart, I - NumStart));
|
2010-09-14 02:15:37 +08:00
|
|
|
unsigned N = atoi(pCodes->back().c_str());
|
2006-02-02 08:23:53 +08:00
|
|
|
// Check that this is a valid matching constraint!
|
|
|
|
if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
|
|
|
|
Type != isInput)
|
|
|
|
return true; // Invalid constraint number.
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2008-10-18 00:47:46 +08: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-14 02:15:37 +08:00
|
|
|
if (isMultipleAlternative) {
|
2015-09-03 23:41:37 +08:00
|
|
|
if (multipleAlternativeIndex >=
|
|
|
|
ConstraintsSoFar[N].multipleAlternatives.size())
|
2015-09-03 23:41:34 +08:00
|
|
|
return true;
|
2010-09-14 02:15:37 +08:00
|
|
|
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();
|
2017-10-25 20:51:32 +08:00
|
|
|
assert(scInfo.MatchingInput >= 0);
|
2010-09-14 02:15:37 +08:00
|
|
|
} else {
|
2015-03-30 04:33:07 +08:00
|
|
|
if (ConstraintsSoFar[N].hasMatchingInput() &&
|
2015-03-30 04:49:03 +08:00
|
|
|
(size_t)ConstraintsSoFar[N].MatchingInput !=
|
|
|
|
ConstraintsSoFar.size())
|
2010-09-14 02:15:37 +08:00
|
|
|
return true;
|
|
|
|
// Note that operand #n has a matching input.
|
|
|
|
ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
|
2017-10-25 20:51:32 +08:00
|
|
|
assert(ConstraintsSoFar[N].MatchingInput >= 0);
|
2010-09-14 02:15:37 +08:00
|
|
|
}
|
|
|
|
} else if (*I == '|') {
|
|
|
|
multipleAlternativeIndex++;
|
|
|
|
pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
|
|
|
|
++I;
|
2011-06-03 03:26:37 +08:00
|
|
|
} else if (*I == '^') {
|
|
|
|
// Multi-letter constraint
|
2011-06-04 06:09:12 +08:00
|
|
|
// FIXME: For now assuming these are 2-character constraints.
|
2016-05-23 21:58:04 +08:00
|
|
|
pCodes->push_back(StringRef(I+1, 2));
|
2011-06-04 06:09:12 +08:00
|
|
|
I += 3;
|
2006-02-01 09:29:47 +08:00
|
|
|
} else {
|
|
|
|
// Single letter constraint.
|
2016-05-23 21:58:04 +08:00
|
|
|
pCodes->push_back(StringRef(I, 1));
|
2006-02-01 09:29:47 +08:00
|
|
|
++I;
|
2006-01-26 08:48:33 +08:00
|
|
|
}
|
2006-02-01 09:29:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-14 02:15:37 +08: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-30 01:29:13 +08:00
|
|
|
InlineAsm::ConstraintInfoVector
|
2009-11-06 18:58:06 +08:00
|
|
|
InlineAsm::ParseConstraints(StringRef Constraints) {
|
2010-10-30 01:29:13 +08:00
|
|
|
ConstraintInfoVector Result;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-02-01 09:29:47 +08:00
|
|
|
// Scan the constraints string.
|
2009-07-25 14:02:13 +08:00
|
|
|
for (StringRef::iterator I = Constraints.begin(),
|
|
|
|
E = Constraints.end(); I != E; ) {
|
2006-02-01 09:29:47 +08:00
|
|
|
ConstraintInfo Info;
|
|
|
|
|
|
|
|
// Find the end of this constraint.
|
2009-07-25 14:02:13 +08:00
|
|
|
StringRef::iterator ConstraintEnd = std::find(I, E, ',');
|
2006-02-01 09:29:47 +08:00
|
|
|
|
|
|
|
if (ConstraintEnd == I || // Empty constraint like ",,"
|
2010-07-26 07:18:32 +08:00
|
|
|
Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
|
2006-02-02 08:23:53 +08:00
|
|
|
Result.clear(); // Erroneous constraint?
|
2006-01-26 10:21:59 +08:00
|
|
|
break;
|
|
|
|
}
|
2006-02-01 09:29:47 +08:00
|
|
|
|
|
|
|
Result.push_back(Info);
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-02-01 09:29:47 +08: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 10:21:59 +08:00
|
|
|
if (I != E) {
|
|
|
|
++I;
|
2015-02-11 05:15:06 +08:00
|
|
|
if (I == E) {
|
|
|
|
Result.clear();
|
|
|
|
break;
|
|
|
|
} // don't allow "xyz,"
|
2006-01-26 10:21:59 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-01-26 10:21:59 +08: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 12:54:35 +08:00
|
|
|
bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
|
2006-01-26 10:21:59 +08:00
|
|
|
if (Ty->isVarArg()) return false;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-10-30 01:29:13 +08:00
|
|
|
ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-01-26 10:21:59 +08:00
|
|
|
// Error parsing constraints.
|
|
|
|
if (Constraints.empty() && !ConstStr.empty()) return false;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-01-26 10:21:59 +08:00
|
|
|
unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
|
2008-05-22 12:46:38 +08:00
|
|
|
unsigned NumIndirect = 0;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-01-26 10:21:59 +08:00
|
|
|
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
|
2006-02-01 09:29:47 +08:00
|
|
|
switch (Constraints[i].Type) {
|
|
|
|
case InlineAsm::isOutput:
|
2008-05-22 12:46:38 +08:00
|
|
|
if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
|
|
|
|
return false; // outputs before inputs and clobbers.
|
2007-04-28 09:02:58 +08:00
|
|
|
if (!Constraints[i].isIndirect) {
|
2006-02-01 09:29:47 +08:00
|
|
|
++NumOutputs;
|
|
|
|
break;
|
|
|
|
}
|
2008-05-22 12:46:38 +08:00
|
|
|
++NumIndirect;
|
2016-08-17 13:10:15 +08:00
|
|
|
LLVM_FALLTHROUGH; // We fall through for Indirect Outputs.
|
2006-02-01 09:29:47 +08:00
|
|
|
case InlineAsm::isInput:
|
2006-01-26 08:48:33 +08:00
|
|
|
if (NumClobbers) return false; // inputs before clobbers.
|
|
|
|
++NumInputs;
|
|
|
|
break;
|
2006-02-01 09:29:47 +08:00
|
|
|
case InlineAsm::isClobber:
|
2006-01-26 08:48:33 +08:00
|
|
|
++NumClobbers;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2008-04-28 07:33:55 +08:00
|
|
|
switch (NumOutputs) {
|
|
|
|
case 0:
|
2010-01-05 21:12:22 +08:00
|
|
|
if (!Ty->getReturnType()->isVoidTy()) return false;
|
2008-04-28 07:33:55 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2010-02-16 19:11:14 +08:00
|
|
|
if (Ty->getReturnType()->isStructTy()) return false;
|
2008-04-28 07:33:55 +08:00
|
|
|
break;
|
|
|
|
default:
|
2011-07-18 12:54:35 +08:00
|
|
|
StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
|
2014-04-09 14:08:46 +08:00
|
|
|
if (!STy || STy->getNumElements() != NumOutputs)
|
2008-04-28 07:33:55 +08:00
|
|
|
return false;
|
|
|
|
break;
|
2018-07-31 03:41:25 +08:00
|
|
|
}
|
|
|
|
|
2006-01-26 08:48:33 +08:00
|
|
|
if (Ty->getNumParams() != NumInputs) return false;
|
2006-01-26 06:26:05 +08:00
|
|
|
return true;
|
|
|
|
}
|