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