2003-07-24 20:20:58 +00:00
|
|
|
//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// 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.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-07-24 20:20:58 +00:00
|
|
|
//
|
|
|
|
// Unified name mangler for CWriter and assembly backends.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-07-25 20:21:20 +00:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2009-09-11 05:40:42 +00:00
|
|
|
#include "llvm/Function.h"
|
2007-09-07 04:06:50 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-07-10 00:04:23 +00:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2009-09-11 05:40:42 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-08-24 21:08:38 +00:00
|
|
|
static char HexDigit(int V) {
|
|
|
|
return V < 10 ? V+'0' : V+'A'-10;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string MangleLetter(unsigned char C) {
|
2005-11-10 21:40:01 +00:00
|
|
|
char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
|
|
|
|
return Result;
|
2003-08-24 21:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// makeNameProper - We don't want identifier names non-C-identifier characters
|
|
|
|
/// in them, so mangle them as appropriate.
|
2005-04-21 23:48:37 +00:00
|
|
|
///
|
2009-07-14 04:50:12 +00:00
|
|
|
std::string Mangler::makeNameProper(const std::string &X,
|
2009-07-20 01:03:30 +00:00
|
|
|
ManglerPrefixTy PrefixTy) {
|
2009-07-14 04:50:12 +00:00
|
|
|
assert(!X.empty() && "Cannot mangle empty strings");
|
2005-09-24 08:24:28 +00:00
|
|
|
|
2005-11-10 19:30:07 +00:00
|
|
|
if (!UseQuotes) {
|
2009-05-05 22:50:29 +00:00
|
|
|
std::string Result;
|
|
|
|
|
2005-11-10 19:30:07 +00:00
|
|
|
// If X does not start with (char)1, add the prefix.
|
2009-05-05 22:50:29 +00:00
|
|
|
bool NeedPrefix = true;
|
2005-11-10 19:30:07 +00:00
|
|
|
std::string::const_iterator I = X.begin();
|
2009-05-05 22:50:29 +00:00
|
|
|
if (*I == 1) {
|
|
|
|
NeedPrefix = false;
|
2005-11-10 19:30:07 +00:00
|
|
|
++I; // Skip over the marker.
|
2009-05-05 22:50:29 +00:00
|
|
|
}
|
2005-11-10 19:30:07 +00:00
|
|
|
|
2009-09-18 16:57:42 +00:00
|
|
|
// Mangle the first letter specially, don't allow numbers unless the target
|
|
|
|
// explicitly allows them.
|
|
|
|
if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
|
2005-11-10 19:30:07 +00:00
|
|
|
Result += MangleLetter(*I++);
|
|
|
|
|
2005-11-10 21:40:01 +00:00
|
|
|
for (std::string::const_iterator E = X.end(); I != E; ++I) {
|
|
|
|
if (!isCharAcceptable(*I))
|
2005-11-10 19:30:07 +00:00
|
|
|
Result += MangleLetter(*I);
|
|
|
|
else
|
|
|
|
Result += *I;
|
2005-11-10 21:40:01 +00:00
|
|
|
}
|
2005-11-10 23:24:26 +00:00
|
|
|
|
2009-07-14 18:17:16 +00:00
|
|
|
if (NeedPrefix) {
|
|
|
|
Result = Prefix + Result;
|
2009-07-20 01:03:30 +00:00
|
|
|
|
2009-07-20 19:41:27 +00:00
|
|
|
if (PrefixTy == Mangler::Private)
|
2009-07-14 18:17:16 +00:00
|
|
|
Result = PrivatePrefix + Result;
|
2009-07-20 19:41:27 +00:00
|
|
|
else if (PrefixTy == Mangler::LinkerPrivate)
|
2009-07-20 01:03:30 +00:00
|
|
|
Result = LinkerPrivatePrefix + Result;
|
2009-07-14 18:17:16 +00:00
|
|
|
}
|
2009-07-20 01:03:30 +00:00
|
|
|
|
2009-05-05 22:50:29 +00: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-10 19:30:07 +00:00
|
|
|
|
2009-05-05 22:50:29 +00: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-10 21:47:01 +00:00
|
|
|
|
2009-05-05 22:50:29 +00:00
|
|
|
// In the common case, we don't need quotes. Handle this quickly.
|
|
|
|
if (!NeedQuotes) {
|
2009-07-14 04:50:12 +00:00
|
|
|
if (!NeedPrefix)
|
|
|
|
return X.substr(1); // Strip off the \001.
|
2009-07-14 18:17:16 +00:00
|
|
|
|
|
|
|
Result = Prefix + X;
|
2009-07-20 01:03:30 +00:00
|
|
|
|
2009-07-20 19:41:27 +00:00
|
|
|
if (PrefixTy == Mangler::Private)
|
2009-07-14 18:17:16 +00:00
|
|
|
Result = PrivatePrefix + Result;
|
2009-07-20 19:41:27 +00:00
|
|
|
else if (PrefixTy == Mangler::LinkerPrivate)
|
2009-07-20 01:03:30 +00:00
|
|
|
Result = LinkerPrivatePrefix + Result;
|
|
|
|
|
2009-07-14 18:17:16 +00:00
|
|
|
return Result;
|
2009-05-05 22:50:29 +00:00
|
|
|
}
|
2009-08-17 23:17:17 +00:00
|
|
|
|
|
|
|
if (NeedPrefix)
|
|
|
|
Result = X.substr(0, I-X.begin());
|
2005-11-10 23:24:26 +00:00
|
|
|
|
2009-05-05 22:50:29 +00: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-10 23:24:26 +00:00
|
|
|
else
|
2009-05-05 22:50:29 +00:00
|
|
|
Result += *I;
|
|
|
|
}
|
|
|
|
|
2009-07-14 18:17:16 +00:00
|
|
|
if (NeedPrefix) {
|
|
|
|
Result = Prefix + Result;
|
2009-07-20 01:03:30 +00:00
|
|
|
|
2009-07-20 19:41:27 +00:00
|
|
|
if (PrefixTy == Mangler::Private)
|
2009-07-14 18:17:16 +00:00
|
|
|
Result = PrivatePrefix + Result;
|
2009-07-20 19:41:27 +00:00
|
|
|
else if (PrefixTy == Mangler::LinkerPrivate)
|
2009-07-20 01:03:30 +00:00
|
|
|
Result = LinkerPrivatePrefix + Result;
|
2009-07-14 18:17:16 +00:00
|
|
|
}
|
2009-07-20 01:03:30 +00:00
|
|
|
|
2009-05-05 22:50:29 +00:00
|
|
|
Result = '"' + Result + '"';
|
2003-08-24 21:08:38 +00:00
|
|
|
return Result;
|
2003-07-24 20:20:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-14 18:17:16 +00: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 00:15:14 +00:00
|
|
|
assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
|
|
|
|
"Intrinsic functions cannot be mangled by Mangler");
|
2009-07-20 01:03:30 +00:00
|
|
|
|
|
|
|
ManglerPrefixTy PrefixTy =
|
2009-07-20 19:41:27 +00:00
|
|
|
(GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
|
|
|
|
GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
|
2009-07-20 01:03:30 +00:00
|
|
|
|
2009-07-14 04:50:12 +00:00
|
|
|
if (GV->hasName())
|
2009-07-22 21:33:09 +00:00
|
|
|
return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
|
2009-07-14 00:01:06 +00: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 01:03:30 +00:00
|
|
|
return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
|
2003-07-24 20:20:58 +00:00
|
|
|
}
|
|
|
|
|
2009-09-11 05:40:42 +00: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 05:51:29 +00:00
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix));
|
2009-09-11 05:40:42 +00: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 01:03:30 +00:00
|
|
|
Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
|
|
|
|
const char *linkerPrivatePrefix)
|
|
|
|
: Prefix(prefix), PrivatePrefix(privatePrefix),
|
|
|
|
LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
|
2009-09-21 14:11:56 +00:00
|
|
|
SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
|
2007-09-07 04:06:50 +00:00
|
|
|
std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
|
2005-11-10 21:40:01 +00: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-13 18:04:46 +00:00
|
|
|
markCharAcceptable('@');
|
2003-07-24 20:20:58 +00:00
|
|
|
}
|