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"
|
2005-11-10 18:48:58 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2003-07-24 20:20:58 +00:00
|
|
|
#include "llvm/Module.h"
|
2009-06-17 22:01:09 +00:00
|
|
|
#include "llvm/System/Atomic.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"
|
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-05-05 22:50:29 +00:00
|
|
|
std::string Mangler::makeNameProper(const std::string &X, const char *Prefix,
|
|
|
|
const char *PrivatePrefix) {
|
2005-11-10 21:40:01 +00:00
|
|
|
if (X.empty()) return X; // Empty names are uniqued by the caller.
|
2005-09-24 08:24:28 +00:00
|
|
|
|
2006-09-07 18:20:41 +00:00
|
|
|
// If PreserveAsmNames is set, names with asm identifiers are not modified.
|
|
|
|
if (PreserveAsmNames && X[0] == 1)
|
|
|
|
return X;
|
|
|
|
|
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
|
|
|
|
2005-11-10 21:40:01 +00:00
|
|
|
// Mangle the first letter specially, don't allow numbers.
|
2005-11-10 19:30:07 +00:00
|
|
|
if (*I >= '0' && *I <= '9')
|
|
|
|
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-05-05 22:50:29 +00:00
|
|
|
if (NeedPrefix) {
|
|
|
|
if (Prefix)
|
|
|
|
Result = Prefix + Result;
|
|
|
|
if (PrivatePrefix)
|
|
|
|
Result = PrivatePrefix + Result;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if (NeedPrefix) {
|
|
|
|
if (Prefix)
|
|
|
|
Result = Prefix + X;
|
2005-11-10 23:24:26 +00:00
|
|
|
else
|
2009-05-05 22:50:29 +00:00
|
|
|
Result = X;
|
|
|
|
if (PrivatePrefix)
|
|
|
|
Result = PrivatePrefix + Result;
|
|
|
|
return Result;
|
|
|
|
} else
|
|
|
|
return X.substr(1);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedPrefix) {
|
|
|
|
if (Prefix)
|
|
|
|
Result = Prefix + X;
|
|
|
|
else
|
|
|
|
Result = X;
|
|
|
|
if (PrivatePrefix)
|
|
|
|
Result = PrivatePrefix + Result;
|
2005-11-10 19:30:07 +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
|
|
|
}
|
|
|
|
|
2004-07-08 22:09:34 +00:00
|
|
|
/// getTypeID - Return a unique ID for the specified LLVM type.
|
|
|
|
///
|
|
|
|
unsigned Mangler::getTypeID(const Type *Ty) {
|
|
|
|
unsigned &E = TypeMap[Ty];
|
|
|
|
if (E == 0) E = ++TypeCounter;
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2003-07-24 20:20:58 +00:00
|
|
|
std::string Mangler::getValueName(const Value *V) {
|
2005-11-10 18:48:58 +00:00
|
|
|
if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
|
|
|
|
return getValueName(GV);
|
|
|
|
|
|
|
|
std::string &Name = Memo[V];
|
|
|
|
if (!Name.empty())
|
|
|
|
return Name; // Return the already-computed name for V.
|
|
|
|
|
|
|
|
// Always mangle local names.
|
|
|
|
Name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
|
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-17 22:39:32 +00:00
|
|
|
std::string Mangler::getValueName(const GlobalValue *GV, const char * Suffix) {
|
2003-07-24 20:20:58 +00:00
|
|
|
// Check to see whether we've already named V.
|
2005-11-10 18:48:58 +00:00
|
|
|
std::string &Name = Memo[GV];
|
|
|
|
if (!Name.empty())
|
|
|
|
return Name; // Return the already-computed name for V.
|
2003-07-24 20:20:58 +00:00
|
|
|
|
2005-11-10 18:48:58 +00:00
|
|
|
// Name mangling occurs as follows:
|
|
|
|
// - If V is an intrinsic function, do not change name at all
|
|
|
|
// - Otherwise, mangling occurs if global collides with existing name.
|
2007-12-03 20:06:50 +00:00
|
|
|
if (isa<Function>(GV) && cast<Function>(GV)->isIntrinsic()) {
|
2008-07-10 00:04:23 +00:00
|
|
|
Name = GV->getNameStart(); // Is an intrinsic function
|
2005-11-15 01:32:03 +00:00
|
|
|
} else if (!GV->hasName()) {
|
|
|
|
// Must mangle the global into a unique ID.
|
|
|
|
unsigned TypeUniqueID = getTypeID(GV->getType());
|
2009-06-17 22:01:09 +00:00
|
|
|
static uint32_t GlobalID = 0;
|
|
|
|
|
|
|
|
unsigned OldID = GlobalID;
|
2009-06-23 18:01:04 +00:00
|
|
|
sys::AtomicIncrement32(&GlobalID);
|
2009-06-17 22:01:09 +00:00
|
|
|
|
|
|
|
Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(OldID);
|
2003-07-24 20:20:58 +00:00
|
|
|
} else {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (GV->hasPrivateLinkage())
|
2009-05-05 22:50:29 +00:00
|
|
|
Name = makeNameProper(GV->getName() + Suffix, Prefix, PrivatePrefix);
|
2009-01-15 20:18:42 +00:00
|
|
|
else
|
2009-05-05 22:50:29 +00:00
|
|
|
Name = makeNameProper(GV->getName() + Suffix, Prefix);
|
2003-07-24 20:20:58 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-11-10 18:48:58 +00:00
|
|
|
return Name;
|
2003-07-24 20:20:58 +00:00
|
|
|
}
|
|
|
|
|
2009-01-15 20:18:42 +00:00
|
|
|
Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix)
|
|
|
|
: Prefix(prefix), PrivatePrefix (privatePrefix), UseQuotes(false),
|
|
|
|
PreserveAsmNames(false), Count(0), TypeCounter(0) {
|
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('.');
|
2003-07-24 20:20:58 +00:00
|
|
|
}
|