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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/InlineAsm.h"
|
2010-03-21 20:37:19 +00:00
|
|
|
#include "ConstantsContext.h"
|
|
|
|
#include "LLVMContextImpl.h"
|
2006-01-24 04:13:11 +00:00
|
|
|
#include "llvm/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() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-06 10:58:06 +00:00
|
|
|
InlineAsm *InlineAsm::get(const FunctionType *Ty, StringRef AsmString,
|
|
|
|
StringRef Constraints, bool hasSideEffects,
|
2009-10-21 23:28:00 +00:00
|
|
|
bool isAlignStack) {
|
2010-03-21 20:37:19 +00:00
|
|
|
InlineAsmKeyType Key(AsmString, Constraints, hasSideEffects, isAlignStack);
|
|
|
|
LLVMContextImpl *pImpl = Ty->getContext().pImpl;
|
|
|
|
return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(Ty), Key);
|
2006-01-25 22:26:05 +00:00
|
|
|
}
|
|
|
|
|
2010-03-21 20:37:19 +00:00
|
|
|
InlineAsm::InlineAsm(const PointerType *Ty, const std::string &asmString,
|
|
|
|
const std::string &constraints, bool hasSideEffects,
|
2009-10-21 23:28:00 +00:00
|
|
|
bool isAlignStack)
|
2010-03-21 20:37:19 +00:00
|
|
|
: Value(Ty, Value::InlineAsmVal),
|
2007-12-17 01:12:55 +00:00
|
|
|
AsmString(asmString),
|
2009-10-21 23:28:00 +00:00
|
|
|
Constraints(constraints), HasSideEffects(hasSideEffects),
|
|
|
|
IsAlignStack(isAlignStack) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const FunctionType *InlineAsm::getFunctionType() const {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy constructor.
|
|
|
|
InlineAsm::ConstraintInfo::ConstraintInfo(const ConstraintInfo &other) :
|
|
|
|
Type(other.Type), isEarlyClobber(other.isEarlyClobber),
|
|
|
|
MatchingInput(other.MatchingInput), isCommutative(other.isCommutative),
|
|
|
|
isIndirect(other.isIndirect), Codes(other.Codes),
|
2010-09-13 18:25:05 +00:00
|
|
|
isMultipleAlternative(other.isMultipleAlternative),
|
2010-09-13 18:15:37 +00:00
|
|
|
multipleAlternatives(other.multipleAlternatives),
|
|
|
|
currentAlternativeIndex(other.currentAlternativeIndex) {
|
|
|
|
}
|
2006-01-25 22:26:05 +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;
|
2006-01-26 00:48:33 +00:00
|
|
|
|
2006-02-01 01:29:47 +00:00
|
|
|
// Initialize
|
2010-09-13 18:15:37 +00:00
|
|
|
isMultipleAlternative = (multipleAlternativeCount > 1 ? true : false);
|
|
|
|
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;
|
|
|
|
} else if (*I == '=') {
|
|
|
|
++I;
|
|
|
|
Type = isOutput;
|
2007-04-28 01:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*I == '*') {
|
|
|
|
isIndirect = true;
|
|
|
|
++I;
|
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;
|
2006-02-02 00:23:53 +00:00
|
|
|
} else if (isdigit(*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;
|
2006-02-01 01:29:47 +00:00
|
|
|
while (I != E && isdigit(*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 {
|
|
|
|
if (ConstraintsSoFar[N].hasMatchingInput())
|
|
|
|
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;
|
|
|
|
if (I == E) { Result.clear(); break; } // don't allow "xyz,"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify - Verify that the specified constraint string is reasonable for the
|
|
|
|
/// specified function type, and otherwise validate the constraint string.
|
2009-11-06 10:58:06 +00:00
|
|
|
bool InlineAsm::Verify(const 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:
|
|
|
|
const StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
|
|
|
|
if (STy == 0 || STy->getNumElements() != NumOutputs)
|
|
|
|
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
|
|
|
|