freebsd-dev/lib/IR/InlineAsm.cpp

296 lines
9.3 KiB
C++
Raw Normal View History

2009-06-02 17:52:33 +00:00
//===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the InlineAsm class.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/InlineAsm.h"
2010-04-02 08:54:30 +00:00
#include "ConstantsContext.h"
#include "LLVMContextImpl.h"
#include "llvm/IR/DerivedTypes.h"
2009-06-02 17:52:33 +00:00
#include <algorithm>
#include <cctype>
using namespace llvm;
// Implement the first virtual method in this class in this file so the
// InlineAsm vtable is emitted here.
InlineAsm::~InlineAsm() {
}
InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,
2009-11-18 14:58:34 +00:00
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);
2009-06-02 17:52:33 +00:00
}
InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
2010-04-02 08:54:30 +00:00
const std::string &constraints, bool hasSideEffects,
bool isAlignStack, AsmDialect asmDialect)
: Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),
AsmString(asmString), Constraints(constraints), FTy(FTy),
HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
Dialect(asmDialect) {
2009-06-02 17:52:33 +00:00
// Do various checks on the constraint string and type.
2010-04-02 08:54:30 +00:00
assert(Verify(getFunctionType(), constraints) &&
"Function type not legal for constraints!");
}
void InlineAsm::destroyConstant() {
getType()->getContext().pImpl->InlineAsms.remove(this);
2010-04-02 08:54:30 +00:00
delete this;
2009-06-02 17:52:33 +00:00
}
FunctionType *InlineAsm::getFunctionType() const {
return FTy;
2009-06-02 17:52:33 +00:00
}
///Default constructor.
InlineAsm::ConstraintInfo::ConstraintInfo() :
Type(isInput), isEarlyClobber(false),
MatchingInput(-1), isCommutative(false),
isIndirect(false), isMultipleAlternative(false),
currentAlternativeIndex(0) {
}
2009-06-02 17:52:33 +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-18 14:58:34 +00:00
bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
2009-10-14 17:57:32 +00:00
StringRef::iterator I = Str.begin(), E = Str.end();
unsigned multipleAlternativeCount = Str.count('|') + 1;
unsigned multipleAlternativeIndex = 0;
ConstraintCodeVector *pCodes = &Codes;
2009-06-02 17:52:33 +00:00
// Initialize
isMultipleAlternative = multipleAlternativeCount > 1;
if (isMultipleAlternative) {
multipleAlternatives.resize(multipleAlternativeCount);
pCodes = &multipleAlternatives[0].Codes;
}
2009-06-02 17:52:33 +00:00
Type = isInput;
isEarlyClobber = false;
MatchingInput = -1;
isCommutative = false;
isIndirect = false;
currentAlternativeIndex = 0;
2009-06-02 17:52:33 +00:00
// Parse prefixes.
if (*I == '~') {
Type = isClobber;
++I;
// '{' must immediately follow '~'.
if (I != E && *I != '{')
return true;
2009-06-02 17:52:33 +00:00
} else if (*I == '=') {
++I;
Type = isOutput;
}
2009-06-02 17:52:33 +00:00
if (*I == '*') {
isIndirect = true;
++I;
}
2009-06-02 17:52:33 +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;
case '&': // Early clobber.
if (Type != isOutput || // Cannot early clobber anything but output.
isEarlyClobber) // Reject &&&&&&
return true;
isEarlyClobber = true;
break;
case '%': // Commutative.
if (Type == isClobber || // Cannot commute clobbers.
isCommutative) // Reject %%%%%
return true;
isCommutative = true;
break;
case '#': // Comment.
case '*': // Register preferencing.
return true; // Not supported.
}
if (!DoneWithModifiers) {
++I;
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-10-14 17:57:32 +00:00
StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
2009-06-02 17:52:33 +00:00
if (ConstraintEnd == E) return true; // "{foo"
pCodes->push_back(StringRef(I, ConstraintEnd+1 - I));
2009-06-02 17:52:33 +00:00
I = ConstraintEnd+1;
} else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
2009-06-02 17:52:33 +00:00
// Maximal munch numbers.
2009-10-14 17:57:32 +00:00
StringRef::iterator NumStart = I;
while (I != E && isdigit(static_cast<unsigned char>(*I)))
2009-06-02 17:52:33 +00:00
++I;
pCodes->push_back(StringRef(NumStart, I - NumStart));
unsigned N = atoi(pCodes->back().c_str());
2009-06-02 17:52:33 +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.
// If Operand N already has a matching input, reject this. An output
// can't be constrained to the same value as multiple inputs.
if (isMultipleAlternative) {
if (multipleAlternativeIndex >=
ConstraintsSoFar[N].multipleAlternatives.size())
return true;
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() &&
(size_t)ConstraintsSoFar[N].MatchingInput !=
ConstraintsSoFar.size())
return true;
// Note that operand #n has a matching input.
ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
}
} else if (*I == '|') {
multipleAlternativeIndex++;
pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
++I;
} else if (*I == '^') {
// Multi-letter constraint
// FIXME: For now assuming these are 2-character constraints.
pCodes->push_back(StringRef(I+1, 2));
I += 3;
2009-06-02 17:52:33 +00:00
} else {
// Single letter constraint.
pCodes->push_back(StringRef(I, 1));
2009-06-02 17:52:33 +00:00
++I;
}
}
return false;
}
/// 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;
}
}
InlineAsm::ConstraintInfoVector
2009-11-18 14:58:34 +00:00
InlineAsm::ParseConstraints(StringRef Constraints) {
ConstraintInfoVector Result;
2009-06-02 17:52:33 +00:00
// Scan the constraints string.
2009-10-14 17:57:32 +00:00
for (StringRef::iterator I = Constraints.begin(),
E = Constraints.end(); I != E; ) {
2009-06-02 17:52:33 +00:00
ConstraintInfo Info;
// Find the end of this constraint.
2009-10-14 17:57:32 +00:00
StringRef::iterator ConstraintEnd = std::find(I, E, ',');
2009-06-02 17:52:33 +00:00
if (ConstraintEnd == I || // Empty constraint like ",,"
Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
2009-06-02 17:52:33 +00:00
Result.clear(); // Erroneous constraint?
break;
}
Result.push_back(Info);
// ConstraintEnd may be either the next comma or the end of the string. In
// the former case, we skip the comma.
I = ConstraintEnd;
if (I != E) {
++I;
if (I == E) {
Result.clear();
break;
} // don't allow "xyz,"
2009-06-02 17:52:33 +00:00
}
}
return Result;
}
/// Verify - Verify that the specified constraint string is reasonable for the
/// specified function type, and otherwise validate the constraint string.
bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
2009-06-02 17:52:33 +00:00
if (Ty->isVarArg()) return false;
ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
2009-06-02 17:52:33 +00:00
// Error parsing constraints.
if (Constraints.empty() && !ConstStr.empty()) return false;
unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
unsigned NumIndirect = 0;
for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
switch (Constraints[i].Type) {
case InlineAsm::isOutput:
if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
return false; // outputs before inputs and clobbers.
if (!Constraints[i].isIndirect) {
++NumOutputs;
break;
}
++NumIndirect;
// FALLTHROUGH for Indirect Outputs.
case InlineAsm::isInput:
if (NumClobbers) return false; // inputs before clobbers.
++NumInputs;
break;
case InlineAsm::isClobber:
++NumClobbers;
break;
}
}
switch (NumOutputs) {
case 0:
2010-01-15 15:37:28 +00:00
if (!Ty->getReturnType()->isVoidTy()) return false;
2009-06-02 17:52:33 +00:00
break;
case 1:
2010-03-03 17:27:15 +00:00
if (Ty->getReturnType()->isStructTy()) return false;
2009-06-02 17:52:33 +00:00
break;
default:
StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
if (!STy || STy->getNumElements() != NumOutputs)
2009-06-02 17:52:33 +00:00
return false;
break;
}
if (Ty->getNumParams() != NumInputs) return false;
return true;
}