#include "llvm/Target/Mangler.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
static bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) {
if ((C < 'a' || C > 'z') &&
(C < 'A' || C > 'Z') &&
(C < '0' || C > '9') &&
C != '_' && C != '$' && C != '@' &&
!(AllowPeriod && C == '.') &&
!(AllowUTF8 && (C & 0x80)))
return false;
return true;
}
static char HexDigit(int V) {
return V < 10 ? V+'0' : V+'A'-10;
}
static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
OutName.push_back('_');
OutName.push_back(HexDigit(C >> 4));
OutName.push_back(HexDigit(C & 15));
OutName.push_back('_');
}
static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
assert(!Str.empty() && "Cannot create an empty MCSymbol");
if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
return true;
bool AllowPeriod = MAI.doesAllowPeriodsInName();
bool AllowUTF8 = MAI.doesAllowUTF8();
for (unsigned i = 0, e = Str.size(); i != e; ++i)
if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
return true;
return false;
}
static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str,
const MCAsmInfo &MAI) {
if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
MangleLetter(OutName, Str[0]);
Str = Str.substr(1);
}
bool AllowPeriod = MAI.doesAllowPeriodsInName();
bool AllowUTF8 = MAI.doesAllowUTF8();
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8))
MangleLetter(OutName, Str[i]);
else
OutName.push_back(Str[i]);
}
}
static void appendMangledQuotedName(SmallVectorImpl<char> &OutName,
StringRef Str) {
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
if (Str[i] == '"' || Str[i] == '\n')
MangleLetter(OutName, Str[i]);
else
OutName.push_back(Str[i]);
}
}
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
const Twine &GVName, ManglerPrefixTy PrefixTy) {
SmallString<256> TmpData;
StringRef Name = GVName.toStringRef(TmpData);
assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
const MCAsmInfo &MAI = Context.getAsmInfo();
if (Name[0] == '\1') {
Name = Name.substr(1);
} else {
if (PrefixTy == Mangler::Private) {
const char *Prefix = MAI.getPrivateGlobalPrefix();
OutName.append(Prefix, Prefix+strlen(Prefix));
} else if (PrefixTy == Mangler::LinkerPrivate) {
const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
OutName.append(Prefix, Prefix+strlen(Prefix));
}
const char *Prefix = MAI.getGlobalPrefix();
if (Prefix[0] == 0)
; else if (Prefix[1] == 0)
OutName.push_back(Prefix[0]); else
OutName.append(Prefix, Prefix+strlen(Prefix)); }
if (!NameNeedsEscaping(Name, MAI) ||
(MAI.doesAllowQuotesInName() &&
Name.find_first_of("\n\"") == StringRef::npos)) {
OutName.append(Name.begin(), Name.end());
return;
}
if (!MAI.doesAllowQuotesInName())
return appendMangledName(OutName, Name, MAI);
assert(Name.find_first_of("\n\"") != StringRef::npos);
return appendMangledQuotedName(OutName, Name);
}
static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
const Function *F, const DataLayout &TD) {
unsigned ArgWords = 0;
for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
AI != AE; ++AI) {
Type *Ty = AI->getType();
if (AI->hasByValAttr())
Ty = cast<PointerType>(Ty)->getElementType();
ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
}
raw_svector_ostream(OutName) << '@' << ArgWords;
}
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
const GlobalValue *GV,
bool isImplicitlyPrivate) {
ManglerPrefixTy PrefixTy = Mangler::Default;
if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
PrefixTy = Mangler::Private;
else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
PrefixTy = Mangler::LinkerPrivate;
if (GV->hasName()) {
getNameWithPrefix(OutName, GV->getName(), PrefixTy);
} else {
unsigned &ID = AnonGlobalIDs[GV];
if (ID == 0) ID = NextAnonGlobalID++;
getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
}
if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) {
if (const Function *F = dyn_cast<Function>(GV)) {
CallingConv::ID CC = F->getCallingConv();
if (CC == CallingConv::X86_FastCall) {
if (OutName[0] == '_')
OutName[0] = '@';
else
OutName.insert(OutName.begin(), '@');
}
FunctionType *FT = F->getFunctionType();
if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
(!FT->isVarArg() || FT->getNumParams() == 0 ||
(FT->getNumParams() == 1 && F->hasStructRetAttr())))
AddFastCallStdCallSuffix(OutName, F, TD);
}
}
}
MCSymbol *Mangler::getSymbol(const GlobalValue *GV) {
SmallString<60> NameStr;
getNameWithPrefix(NameStr, GV, false);
return Context.GetOrCreateSymbol(NameStr.str());
}