2003-07-25 04:20:58 +08:00
|
|
|
//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +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.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-07-25 04:20:58 +08:00
|
|
|
//
|
|
|
|
// Unified name mangler for CWriter and assembly backends.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-07-26 04:21:20 +08:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2009-09-11 13:40:42 +08:00
|
|
|
#include "llvm/Function.h"
|
2007-09-07 12:06:50 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-07-10 08:04:23 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2009-09-11 13:40:42 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-12-15 05:35:53 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2003-08-25 05:08:38 +08:00
|
|
|
static char HexDigit(int V) {
|
|
|
|
return V < 10 ? V+'0' : V+'A'-10;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string MangleLetter(unsigned char C) {
|
2005-11-11 05:40:01 +08:00
|
|
|
char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
|
|
|
|
return Result;
|
2003-08-25 05:08:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// makeNameProper - We don't want identifier names non-C-identifier characters
|
|
|
|
/// in them, so mangle them as appropriate.
|
2005-04-22 07:48:37 +08:00
|
|
|
///
|
2009-07-14 12:50:12 +08:00
|
|
|
std::string Mangler::makeNameProper(const std::string &X,
|
2009-07-20 09:03:30 +08:00
|
|
|
ManglerPrefixTy PrefixTy) {
|
2009-07-14 12:50:12 +08:00
|
|
|
assert(!X.empty() && "Cannot mangle empty strings");
|
2005-09-24 16:24:28 +08:00
|
|
|
|
2005-11-11 03:30:07 +08:00
|
|
|
if (!UseQuotes) {
|
2009-05-06 06:50:29 +08:00
|
|
|
std::string Result;
|
|
|
|
|
2005-11-11 03:30:07 +08:00
|
|
|
// If X does not start with (char)1, add the prefix.
|
2009-05-06 06:50:29 +08:00
|
|
|
bool NeedPrefix = true;
|
2005-11-11 03:30:07 +08:00
|
|
|
std::string::const_iterator I = X.begin();
|
2009-05-06 06:50:29 +08:00
|
|
|
if (*I == 1) {
|
|
|
|
NeedPrefix = false;
|
2005-11-11 03:30:07 +08:00
|
|
|
++I; // Skip over the marker.
|
2009-05-06 06:50:29 +08:00
|
|
|
}
|
2005-11-11 03:30:07 +08:00
|
|
|
|
2009-09-19 00:57:42 +08:00
|
|
|
// Mangle the first letter specially, don't allow numbers unless the target
|
|
|
|
// explicitly allows them.
|
|
|
|
if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
|
2005-11-11 03:30:07 +08:00
|
|
|
Result += MangleLetter(*I++);
|
|
|
|
|
2005-11-11 05:40:01 +08:00
|
|
|
for (std::string::const_iterator E = X.end(); I != E; ++I) {
|
|
|
|
if (!isCharAcceptable(*I))
|
2005-11-11 03:30:07 +08:00
|
|
|
Result += MangleLetter(*I);
|
|
|
|
else
|
|
|
|
Result += *I;
|
2005-11-11 05:40:01 +08:00
|
|
|
}
|
2005-11-11 07:24:26 +08:00
|
|
|
|
2009-07-15 02:17:16 +08:00
|
|
|
if (NeedPrefix) {
|
|
|
|
Result = Prefix + Result;
|
2009-07-20 09:03:30 +08:00
|
|
|
|
2009-07-21 03:41:27 +08:00
|
|
|
if (PrefixTy == Mangler::Private)
|
2009-07-15 02:17:16 +08:00
|
|
|
Result = PrivatePrefix + Result;
|
2009-07-21 03:41:27 +08:00
|
|
|
else if (PrefixTy == Mangler::LinkerPrivate)
|
2009-07-20 09:03:30 +08:00
|
|
|
Result = LinkerPrivatePrefix + Result;
|
2009-07-15 02:17:16 +08:00
|
|
|
}
|
2009-07-20 09:03:30 +08:00
|
|
|
|
2009-05-06 06:50:29 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NeedPrefix = true;
|
|
|
|
bool NeedQuotes = false;
|
|
|
|
std::string Result;
|
|
|
|
std::string::const_iterator I = X.begin();
|
|
|
|
if (*I == 1) {
|
|
|
|
NeedPrefix = false;
|
|
|
|
++I; // Skip over the marker.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the first character is a number, we need quotes.
|
|
|
|
if (*I >= '0' && *I <= '9')
|
|
|
|
NeedQuotes = true;
|
2005-11-11 03:30:07 +08:00
|
|
|
|
2009-05-06 06:50:29 +08:00
|
|
|
// Do an initial scan of the string, checking to see if we need quotes or
|
|
|
|
// to escape a '"' or not.
|
|
|
|
if (!NeedQuotes)
|
|
|
|
for (std::string::const_iterator E = X.end(); I != E; ++I)
|
|
|
|
if (!isCharAcceptable(*I)) {
|
|
|
|
NeedQuotes = true;
|
|
|
|
break;
|
|
|
|
}
|
2005-11-11 05:47:01 +08:00
|
|
|
|
2009-05-06 06:50:29 +08:00
|
|
|
// In the common case, we don't need quotes. Handle this quickly.
|
|
|
|
if (!NeedQuotes) {
|
2009-07-14 12:50:12 +08:00
|
|
|
if (!NeedPrefix)
|
|
|
|
return X.substr(1); // Strip off the \001.
|
2009-07-15 02:17:16 +08:00
|
|
|
|
|
|
|
Result = Prefix + X;
|
2009-07-20 09:03:30 +08:00
|
|
|
|
2009-07-21 03:41:27 +08:00
|
|
|
if (PrefixTy == Mangler::Private)
|
2009-07-15 02:17:16 +08:00
|
|
|
Result = PrivatePrefix + Result;
|
2009-07-21 03:41:27 +08:00
|
|
|
else if (PrefixTy == Mangler::LinkerPrivate)
|
2009-07-20 09:03:30 +08:00
|
|
|
Result = LinkerPrivatePrefix + Result;
|
|
|
|
|
2009-07-15 02:17:16 +08:00
|
|
|
return Result;
|
2009-05-06 06:50:29 +08:00
|
|
|
}
|
2009-08-18 07:17:17 +08:00
|
|
|
|
|
|
|
if (NeedPrefix)
|
|
|
|
Result = X.substr(0, I-X.begin());
|
2005-11-11 07:24:26 +08:00
|
|
|
|
2009-05-06 06:50:29 +08:00
|
|
|
// Otherwise, construct the string the expensive way.
|
|
|
|
for (std::string::const_iterator E = X.end(); I != E; ++I) {
|
|
|
|
if (*I == '"')
|
|
|
|
Result += "_QQ_";
|
|
|
|
else if (*I == '\n')
|
|
|
|
Result += "_NL_";
|
2005-11-11 07:24:26 +08:00
|
|
|
else
|
2009-05-06 06:50:29 +08:00
|
|
|
Result += *I;
|
|
|
|
}
|
|
|
|
|
2009-07-15 02:17:16 +08:00
|
|
|
if (NeedPrefix) {
|
|
|
|
Result = Prefix + Result;
|
2009-07-20 09:03:30 +08:00
|
|
|
|
2009-07-21 03:41:27 +08:00
|
|
|
if (PrefixTy == Mangler::Private)
|
2009-07-15 02:17:16 +08:00
|
|
|
Result = PrivatePrefix + Result;
|
2009-07-21 03:41:27 +08:00
|
|
|
else if (PrefixTy == Mangler::LinkerPrivate)
|
2009-07-20 09:03:30 +08:00
|
|
|
Result = LinkerPrivatePrefix + Result;
|
2009-07-15 02:17:16 +08:00
|
|
|
}
|
2009-07-20 09:03:30 +08:00
|
|
|
|
2009-05-06 06:50:29 +08:00
|
|
|
Result = '"' + Result + '"';
|
2003-08-25 05:08:38 +08:00
|
|
|
return Result;
|
2003-07-25 04:20:58 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 02:17:16 +08:00
|
|
|
/// getMangledName - Returns the mangled name of V, an LLVM Value,
|
|
|
|
/// in the current module. If 'Suffix' is specified, the name ends with the
|
|
|
|
/// specified suffix. If 'ForcePrivate' is specified, the label is specified
|
|
|
|
/// to have a private label prefix.
|
|
|
|
///
|
|
|
|
std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
|
|
|
|
bool ForcePrivate) {
|
2009-07-14 08:15:14 +08:00
|
|
|
assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
|
|
|
|
"Intrinsic functions cannot be mangled by Mangler");
|
2009-07-20 09:03:30 +08:00
|
|
|
|
|
|
|
ManglerPrefixTy PrefixTy =
|
2009-07-21 03:41:27 +08:00
|
|
|
(GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
|
|
|
|
GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
|
2009-07-20 09:03:30 +08:00
|
|
|
|
2009-07-14 12:50:12 +08:00
|
|
|
if (GV->hasName())
|
2009-07-23 05:33:09 +08:00
|
|
|
return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
|
2009-07-14 08:01:06 +08:00
|
|
|
|
|
|
|
// Get the ID for the global, assigning a new one if we haven't got one
|
|
|
|
// already.
|
|
|
|
unsigned &ID = AnonGlobalIDs[GV];
|
|
|
|
if (ID == 0) ID = NextAnonGlobalID++;
|
|
|
|
|
|
|
|
// Must mangle the global into a unique ID.
|
2009-07-20 09:03:30 +08:00
|
|
|
return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
|
2003-07-25 04:20:58 +08:00
|
|
|
}
|
|
|
|
|
2009-09-11 13:40:42 +08:00
|
|
|
|
|
|
|
/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
|
|
|
|
/// and the specified global variable's name. If the global variable doesn't
|
|
|
|
/// have a name, this fills in a unique name for the global.
|
|
|
|
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
|
|
|
|
const GlobalValue *GV,
|
|
|
|
bool isImplicitlyPrivate) {
|
|
|
|
|
|
|
|
// If the global is anonymous or not led with \1, then add the appropriate
|
|
|
|
// prefix.
|
|
|
|
if (!GV->hasName() || GV->getName()[0] != '\1') {
|
|
|
|
if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
|
|
|
|
OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
|
|
|
|
else if (GV->hasLinkerPrivateLinkage())
|
|
|
|
OutName.append(LinkerPrivatePrefix,
|
|
|
|
LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
|
2009-09-11 13:51:29 +08:00
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix));
|
2009-09-11 13:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the global has a name, just append it now.
|
|
|
|
if (GV->hasName()) {
|
|
|
|
StringRef Name = GV->getName();
|
|
|
|
|
|
|
|
// Strip off the prefix marker if present.
|
|
|
|
if (Name[0] != '\1')
|
|
|
|
OutName.append(Name.begin(), Name.end());
|
|
|
|
else
|
|
|
|
OutName.append(Name.begin()+1, Name.end());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the global variable doesn't have a name, return a unique name for the
|
|
|
|
// global based on a numbering.
|
|
|
|
|
|
|
|
// Get the ID for the global, assigning a new one if we haven't got one
|
|
|
|
// already.
|
|
|
|
unsigned &ID = AnonGlobalIDs[GV];
|
|
|
|
if (ID == 0) ID = NextAnonGlobalID++;
|
|
|
|
|
|
|
|
// Must mangle the global into a unique ID.
|
|
|
|
raw_svector_ostream(OutName) << "__unnamed_" << ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-20 09:03:30 +08:00
|
|
|
Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
|
|
|
|
const char *linkerPrivatePrefix)
|
|
|
|
: Prefix(prefix), PrivatePrefix(privatePrefix),
|
|
|
|
LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
|
2009-07-15 12:50:47 +08:00
|
|
|
NextAnonGlobalID(1) {
|
2007-09-07 12:06:50 +08:00
|
|
|
std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
|
2005-11-11 05:40:01 +08:00
|
|
|
|
|
|
|
// Letters and numbers are acceptable.
|
|
|
|
for (unsigned char X = 'a'; X <= 'z'; ++X)
|
|
|
|
markCharAcceptable(X);
|
|
|
|
for (unsigned char X = 'A'; X <= 'Z'; ++X)
|
|
|
|
markCharAcceptable(X);
|
|
|
|
for (unsigned char X = '0'; X <= '9'; ++X)
|
|
|
|
markCharAcceptable(X);
|
|
|
|
|
|
|
|
// These chars are acceptable.
|
|
|
|
markCharAcceptable('_');
|
|
|
|
markCharAcceptable('$');
|
|
|
|
markCharAcceptable('.');
|
2009-09-14 02:04:46 +08:00
|
|
|
markCharAcceptable('@');
|
2003-07-25 04:20:58 +08:00
|
|
|
}
|