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"
19 #include "llvm/Support/raw_ostream.h"
22 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
23 /// and the specified name as the global variable name. GVName must not be
25 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
26 const Twine &GVName, ManglerPrefixTy PrefixTy) {
27 SmallString<256> TmpData;
28 StringRef Name = GVName.toStringRef(TmpData);
29 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
31 // If the global name is not led with \1, add the appropriate prefixes.
32 if (Name[0] != '\1') {
33 if (PrefixTy == Mangler::Private) {
34 const char *Prefix = MAI.getPrivateGlobalPrefix();
35 OutName.append(Prefix, Prefix+strlen(Prefix));
36 } else if (PrefixTy == Mangler::LinkerPrivate) {
37 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
38 OutName.append(Prefix, Prefix+strlen(Prefix));
41 const char *Prefix = MAI.getGlobalPrefix();
43 ; // Common noop, no prefix.
44 else if (Prefix[1] == 0)
45 OutName.push_back(Prefix[0]); // Common, one character prefix.
47 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
49 Name = Name.substr(1);
52 OutName.append(Name.begin(), Name.end());
56 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
57 /// and the specified global variable's name. If the global variable doesn't
58 /// have a name, this fills in a unique name for the global.
59 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
60 const GlobalValue *GV,
61 bool isImplicitlyPrivate) {
62 // If this global has a name, handle it simply.
64 ManglerPrefixTy PrefixTy = Mangler::Default;
65 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
66 PrefixTy = Mangler::Private;
67 else if (GV->hasLinkerPrivateLinkage())
68 PrefixTy = Mangler::LinkerPrivate;
70 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
73 // If the global variable doesn't have a name, return a unique name for the
74 // global based on a numbering.
75 if (GV->hasPrivateLinkage() || isImplicitlyPrivate) {
76 const char *Prefix = MAI.getPrivateGlobalPrefix();
77 OutName.append(Prefix, Prefix+strlen(Prefix));
78 } else if (GV->hasLinkerPrivateLinkage()) {
79 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
80 OutName.append(Prefix, Prefix+strlen(Prefix));
83 const char *Prefix = MAI.getGlobalPrefix();
85 ; // Common noop, no prefix.
86 else if (Prefix[1] == 0)
87 OutName.push_back(Prefix[0]); // Common, one character prefix.
89 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
91 // Get the ID for the global, assigning a new one if we haven't got one
93 unsigned &ID = AnonGlobalIDs[GV];
94 if (ID == 0) ID = NextAnonGlobalID++;
96 // Must mangle the global into a unique ID.
97 raw_svector_ostream(OutName) << "__unnamed_" << ID;
100 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
101 /// and the specified global variable's name. If the global variable doesn't
102 /// have a name, this fills in a unique name for the global.
103 std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
104 bool isImplicitlyPrivate) {
106 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
107 return std::string(Buf.begin(), Buf.end());