2003-07-25 04:20:58 +08:00
|
|
|
//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-07-25 04:20:58 +08:00
|
|
|
//
|
2010-01-17 05:08:46 +08:00
|
|
|
// Unified name mangler for assembly backends.
|
2003-07-25 04:20:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-17 05:57:06 +08:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2010-01-17 05:08:46 +08:00
|
|
|
#include "llvm/GlobalValue.h"
|
2010-01-18 02:22:35 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-01-13 13:02:57 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-01-17 05:08:46 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2009-09-11 13:40:42 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-12-15 05:35:53 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2010-01-13 15:01:09 +08: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 20:45:23 +08:00
|
|
|
StringRef Name = GVName.toStringRef(TmpData);
|
2010-01-13 15:01:09 +08: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-18 02:22:35 +08: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 15:01:09 +08: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-18 02:22:35 +08:00
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
|
2010-01-13 15:01:09 +08:00
|
|
|
} else {
|
|
|
|
Name = Name.substr(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutName.append(Name.begin(), Name.end());
|
|
|
|
}
|
|
|
|
|
2009-09-11 13:40:42 +08: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-13 15:01:09 +08:00
|
|
|
// If this global has a name, handle it simply.
|
|
|
|
if (GV->hasName()) {
|
|
|
|
ManglerPrefixTy PrefixTy = Mangler::Default;
|
2009-09-11 13:40:42 +08:00
|
|
|
if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
|
2010-01-13 15:01:09 +08:00
|
|
|
PrefixTy = Mangler::Private;
|
2009-09-11 13:40:42 +08:00
|
|
|
else if (GV->hasLinkerPrivateLinkage())
|
2010-01-13 15:01:09 +08:00
|
|
|
PrefixTy = Mangler::LinkerPrivate;
|
2009-09-11 13:40:42 +08:00
|
|
|
|
2010-01-13 15:01:09 +08:00
|
|
|
return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
|
2009-09-11 13:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the global variable doesn't have a name, return a unique name for the
|
|
|
|
// global based on a numbering.
|
2010-01-18 02:22:35 +08:00
|
|
|
if (GV->hasPrivateLinkage() || isImplicitlyPrivate) {
|
|
|
|
const char *Prefix = MAI.getPrivateGlobalPrefix();
|
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix));
|
|
|
|
} else if (GV->hasLinkerPrivateLinkage()) {
|
|
|
|
const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
|
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix));
|
|
|
|
}
|
2009-09-11 13:40:42 +08:00
|
|
|
|
2010-01-18 02:22:35 +08:00
|
|
|
const char *Prefix = MAI.getGlobalPrefix();
|
|
|
|
if (Prefix[0] == 0)
|
|
|
|
; // Common noop, no prefix.
|
|
|
|
else if (Prefix[1] == 0)
|
|
|
|
OutName.push_back(Prefix[0]); // Common, one character prefix.
|
|
|
|
else
|
|
|
|
OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
|
2010-01-13 15:01:09 +08:00
|
|
|
|
2009-09-11 13:40:42 +08: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.
|
|
|
|
raw_svector_ostream(OutName) << "__unnamed_" << ID;
|
|
|
|
}
|
|
|
|
|
2010-01-17 02:06:34 +08: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());
|
|
|
|
}
|