1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Unified name mangler for assembly backends.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/Mangler.h"
15 #include "llvm/GlobalValue.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
21 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
22 /// and the specified name as the global variable name. GVName must not be
24 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
25 const Twine &GVName, ManglerPrefixTy PrefixTy) {
26 SmallString<256> TmpData;
27 StringRef Name = GVName.toStringRef(TmpData);
28 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
30 // If the global name is not led with \1, add the appropriate prefixes.
31 if (Name[0] != '\1') {
32 if (PrefixTy == Mangler::Private) {
33 const char *Prefix = MAI.getPrivateGlobalPrefix();
34 OutName.append(Prefix, Prefix+strlen(Prefix));
35 } else if (PrefixTy == Mangler::LinkerPrivate) {
36 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
37 OutName.append(Prefix, Prefix+strlen(Prefix));
40 const char *Prefix = MAI.getGlobalPrefix();
42 ; // Common noop, no prefix.
43 else if (Prefix[1] == 0)
44 OutName.push_back(Prefix[0]); // Common, one character prefix.
46 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
48 Name = Name.substr(1);
51 OutName.append(Name.begin(), Name.end());
55 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
56 /// and the specified global variable's name. If the global variable doesn't
57 /// have a name, this fills in a unique name for the global.
58 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
59 const GlobalValue *GV,
60 bool isImplicitlyPrivate) {
61 ManglerPrefixTy PrefixTy = Mangler::Default;
62 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
63 PrefixTy = Mangler::Private;
64 else if (GV->hasLinkerPrivateLinkage())
65 PrefixTy = Mangler::LinkerPrivate;
67 // If this global has a name, handle it simply.
69 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
71 // Get the ID for the global, assigning a new one if we haven't got one
73 unsigned &ID = AnonGlobalIDs[GV];
74 if (ID == 0) ID = NextAnonGlobalID++;
76 // Must mangle the global into a unique ID.
77 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
80 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
81 /// and the specified global variable's name. If the global variable doesn't
82 /// have a name, this fills in a unique name for the global.
83 std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
84 bool isImplicitlyPrivate) {
86 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
87 return std::string(Buf.begin(), Buf.end());