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
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and 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"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/StringExtras.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
|
|
|
///
|
2005-09-24 08:24:28 +00:00
|
|
|
std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
|
2003-08-24 21:08:38 +00:00
|
|
|
std::string Result;
|
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) {
|
|
|
|
// If X does not start with (char)1, add the prefix.
|
|
|
|
std::string::const_iterator I = X.begin();
|
|
|
|
if (*I != 1)
|
|
|
|
Result = Prefix;
|
2003-08-24 21:08:38 +00:00
|
|
|
else
|
2005-11-10 19:30:07 +00:00
|
|
|
++I; // Skip over the marker.
|
|
|
|
|
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 19:30:07 +00:00
|
|
|
} else {
|
|
|
|
bool NeedsQuotes = false;
|
|
|
|
|
|
|
|
std::string::const_iterator I = X.begin();
|
2005-11-10 23:24:26 +00:00
|
|
|
if (*I == 1)
|
2005-11-10 19:30:07 +00:00
|
|
|
++I; // Skip over the marker.
|
2005-11-10 23:24:26 +00:00
|
|
|
|
2005-11-10 19:30:07 +00:00
|
|
|
// If the first character is a number, we need quotes.
|
|
|
|
if (*I >= '0' && *I <= '9')
|
|
|
|
NeedsQuotes = true;
|
|
|
|
|
2005-11-10 21:47:01 +00:00
|
|
|
// Do an initial scan of the string, checking to see if we need quotes or
|
|
|
|
// to escape a '"' or not.
|
|
|
|
if (!NeedsQuotes)
|
|
|
|
for (std::string::const_iterator E = X.end(); I != E; ++I)
|
|
|
|
if (!isCharAcceptable(*I)) {
|
|
|
|
NeedsQuotes = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the common case, we don't need quotes. Handle this quickly.
|
2005-11-10 23:24:26 +00:00
|
|
|
if (!NeedsQuotes) {
|
|
|
|
if (*X.begin() != 1)
|
|
|
|
return Prefix+X;
|
|
|
|
else
|
|
|
|
return X.substr(1);
|
|
|
|
}
|
2005-11-10 21:47:01 +00:00
|
|
|
|
|
|
|
// Otherwise, construct the string the expensive way.
|
|
|
|
I = X.begin();
|
2005-11-10 23:24:26 +00:00
|
|
|
|
|
|
|
// If X does not start with (char)1, add the prefix.
|
|
|
|
if (*I != 1)
|
|
|
|
Result = Prefix;
|
|
|
|
else
|
|
|
|
++I; // Skip the marker if present.
|
|
|
|
|
2005-11-10 21:47:01 +00:00
|
|
|
for (std::string::const_iterator E = X.end(); I != E; ++I) {
|
2005-11-10 19:30:07 +00:00
|
|
|
if (*I == '"')
|
|
|
|
Result += "_QQ_";
|
2005-11-10 21:47:01 +00:00
|
|
|
else
|
2005-11-10 19:30:07 +00:00
|
|
|
Result += *I;
|
2005-11-10 21:47:01 +00:00
|
|
|
}
|
|
|
|
Result = '"' + Result + '"';
|
2005-11-10 19:30:07 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string Mangler::getValueName(const GlobalValue *GV) {
|
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.
|
|
|
|
if (isa<Function>(GV) && cast<Function>(GV)->getIntrinsicID()) {
|
|
|
|
Name = GV->getName(); // 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());
|
|
|
|
static unsigned GlobalID = 0;
|
|
|
|
Name = "__unnamed_" + utostr(TypeUniqueID) + "_" + utostr(GlobalID++);
|
2005-11-10 18:48:58 +00:00
|
|
|
} else if (!MangledGlobals.count(GV)) {
|
|
|
|
Name = makeNameProper(GV->getName(), Prefix);
|
2003-07-24 20:20:58 +00:00
|
|
|
} else {
|
2005-11-10 18:48:58 +00:00
|
|
|
unsigned TypeUniqueID = getTypeID(GV->getType());
|
|
|
|
Name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(GV->getName());
|
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
|
|
|
}
|
|
|
|
|
2004-02-14 00:30:23 +00:00
|
|
|
void Mangler::InsertName(GlobalValue *GV,
|
|
|
|
std::map<std::string, GlobalValue*> &Names) {
|
2005-11-15 01:32:03 +00:00
|
|
|
if (!GV->hasName()) // We must mangle unnamed globals.
|
2004-02-14 00:30:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Figure out if this is already used.
|
|
|
|
GlobalValue *&ExistingValue = Names[GV->getName()];
|
|
|
|
if (!ExistingValue) {
|
|
|
|
ExistingValue = GV;
|
|
|
|
} else {
|
|
|
|
// If GV is external but the existing one is static, mangle the existing one
|
2006-09-14 18:23:27 +00:00
|
|
|
if ((GV->hasExternalLinkage() || GV->hasDLLImportLinkage()) &&
|
|
|
|
!(ExistingValue->hasExternalLinkage() || ExistingValue->hasDLLImportLinkage())) {
|
2004-02-14 00:30:23 +00:00
|
|
|
MangledGlobals.insert(ExistingValue);
|
|
|
|
ExistingValue = GV;
|
2006-09-14 18:23:27 +00:00
|
|
|
} else if ((GV->hasExternalLinkage() ||
|
|
|
|
GV->hasDLLImportLinkage()) &&
|
|
|
|
(ExistingValue->hasExternalLinkage() ||
|
|
|
|
ExistingValue->hasDLLImportLinkage()) &&
|
2007-01-30 20:08:39 +00:00
|
|
|
GV->isDeclaration() &&
|
|
|
|
ExistingValue->isDeclaration()) {
|
2006-02-13 21:43:26 +00:00
|
|
|
// If the two globals both have external inkage, and are both external,
|
|
|
|
// don't mangle either of them, we just have some silly type mismatch.
|
2004-02-14 00:30:23 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, mangle GV
|
|
|
|
MangledGlobals.insert(GV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-10 19:02:18 +00:00
|
|
|
Mangler::Mangler(Module &M, const char *prefix)
|
2006-09-07 18:20:41 +00:00
|
|
|
: Prefix(prefix), UseQuotes(false), PreserveAsmNames(false),
|
|
|
|
Count(0), TypeCounter(0) {
|
2005-11-10 21:40:01 +00:00
|
|
|
std::fill(AcceptableChars,
|
|
|
|
AcceptableChars+sizeof(AcceptableChars)/sizeof(AcceptableChars[0]),
|
|
|
|
0);
|
|
|
|
|
|
|
|
// 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
|
|
|
// Calculate which global values have names that will collide when we throw
|
|
|
|
// away type information.
|
2004-02-14 00:30:23 +00:00
|
|
|
std::map<std::string, GlobalValue*> Names;
|
2003-07-24 20:20:58 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2004-02-14 00:30:23 +00:00
|
|
|
InsertName(I, Names);
|
2005-03-15 04:54:21 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
|
2004-02-14 00:30:23 +00:00
|
|
|
InsertName(I, Names);
|
2003-07-24 20:20:58 +00:00
|
|
|
}
|
2006-06-07 23:03:13 +00:00
|
|
|
|
|
|
|
// Cause this file to be linked in when Support/Mangler.h is #included
|
|
|
|
DEFINING_FILE_FOR(Mangler)
|