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
|
|
|
//
|
2010-01-16 21:08:46 +00:00
|
|
|
// Unified name mangler for assembly backends.
|
2003-07-24 20:20:58 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-16 21:57:06 +00:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2010-01-16 21:08:46 +00:00
|
|
|
#include "llvm/GlobalValue.h"
|
2010-01-17 18:22:35 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-01-13 05:02:57 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-01-16 21:08:46 +00:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2003-12-14 21:35:53 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2010-01-13 07:01:09 +00:00
|
|
|
/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
|
|
|
|
/// and the specified name as the global variable name. GVName must not be
|
|
|
|
/// empty.
|
|
|
|
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
|
|
|
|
const Twine &GVName, ManglerPrefixTy PrefixTy) {
|
|
|
|
SmallString<256> TmpData;
|
2010-01-13 12:45:23 +00:00
|
|
|
StringRef Name = GVName.toStringRef(TmpData);
|
2010-01-13 07:01:09 +00:00
|
|
|
assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
|
|
|
|
|
|
|
|
// If the global name is not led with \1, add the appropriate prefixes.
|
|
|
|
if (Name[0] != '\1') {
|
2010-01-17 18:22:35 +00:00
|
|
|
if (PrefixTy == Mangler::Private) {
|
|
|
|
const char *Prefix = MAI.getPrivateGlobalPrefix();
|
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix));
|
|
|
|
} else if (PrefixTy == Mangler::LinkerPrivate) {
|
|
|
|
const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
|
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Prefix = MAI.getGlobalPrefix();
|
2010-01-13 07:01:09 +00:00
|
|
|
if (Prefix[0] == 0)
|
|
|
|
; // Common noop, no prefix.
|
|
|
|
else if (Prefix[1] == 0)
|
|
|
|
OutName.push_back(Prefix[0]); // Common, one character prefix.
|
|
|
|
else
|
2010-01-17 18:22:35 +00:00
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
|
2010-01-13 07:01:09 +00:00
|
|
|
} else {
|
|
|
|
Name = Name.substr(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutName.append(Name.begin(), Name.end());
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-01-17 18:52:16 +00:00
|
|
|
ManglerPrefixTy PrefixTy = Mangler::Default;
|
|
|
|
if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
|
|
|
|
PrefixTy = Mangler::Private;
|
|
|
|
else if (GV->hasLinkerPrivateLinkage())
|
|
|
|
PrefixTy = Mangler::LinkerPrivate;
|
|
|
|
|
2010-01-13 07:01:09 +00:00
|
|
|
// If this global has a name, handle it simply.
|
2010-01-17 18:52:16 +00:00
|
|
|
if (GV->hasName())
|
2010-01-13 07:01:09 +00:00
|
|
|
return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
|
|
|
|
|
2009-09-11 05:40:42 +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.
|
2010-01-17 18:52:16 +00:00
|
|
|
getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
|
2009-09-11 05:40:42 +00:00
|
|
|
}
|
|
|
|
|
2010-01-16 18:06:34 +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.
|
|
|
|
std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
|
|
|
|
bool isImplicitlyPrivate) {
|
|
|
|
SmallString<64> Buf;
|
|
|
|
getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
|
|
|
|
return std::string(Buf.begin(), Buf.end());
|
|
|
|
}
|