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 CWriter and assembly backends.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Mangler.h"
15 #include "llvm/Function.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/raw_ostream.h"
23 static char HexDigit(int V) {
24 return V < 10 ? V+'0' : V+'A'-10;
27 static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) {
28 OutName.push_back('_');
29 OutName.push_back(HexDigit(C >> 4));
30 OutName.push_back(HexDigit(C & 15));
31 OutName.push_back('_');
34 /// makeNameProper - We don't want identifier names non-C-identifier characters
35 /// in them, so mangle them as appropriate.
37 void Mangler::makeNameProper(SmallVectorImpl<char> &OutName,
39 ManglerPrefixTy PrefixTy) {
40 SmallString<256> TmpData;
41 TheName.toVector(TmpData);
42 StringRef X = TmpData.str();
43 assert(!X.empty() && "Cannot mangle empty strings");
46 // If X does not start with (char)1, add the prefix.
47 StringRef::iterator I = X.begin();
49 ++I; // Skip over the no-prefix marker.
51 if (PrefixTy == Mangler::Private)
52 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
53 else if (PrefixTy == Mangler::LinkerPrivate)
54 OutName.append(LinkerPrivatePrefix,
55 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
56 OutName.append(Prefix, Prefix+strlen(Prefix));
59 // Mangle the first letter specially, don't allow numbers unless the target
60 // explicitly allows them.
61 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
62 MangleLetter(OutName, *I++);
64 for (StringRef::iterator E = X.end(); I != E; ++I) {
65 if (!isCharAcceptable(*I))
66 MangleLetter(OutName, *I);
68 OutName.push_back(*I);
73 bool NeedPrefix = true;
74 bool NeedQuotes = false;
75 StringRef::iterator I = X.begin();
78 ++I; // Skip over the marker.
81 // If the first character is a number, we need quotes.
82 if (*I >= '0' && *I <= '9')
85 // Do an initial scan of the string, checking to see if we need quotes or
86 // to escape a '"' or not.
88 for (StringRef::iterator E = X.end(); I != E; ++I)
89 if (!isCharAcceptable(*I)) {
94 // In the common case, we don't need quotes. Handle this quickly.
97 OutName.append(X.begin()+1, X.end()); // Strip off the \001.
101 if (PrefixTy == Mangler::Private)
102 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
103 else if (PrefixTy == Mangler::LinkerPrivate)
104 OutName.append(LinkerPrivatePrefix,
105 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
108 ; // Common noop, no prefix.
109 else if (Prefix[1] == 0)
110 OutName.push_back(Prefix[0]); // Common, one character prefix.
112 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
113 OutName.append(X.begin(), X.end());
117 // Add leading quote.
118 OutName.push_back('"');
120 // Add prefixes unless disabled.
122 if (PrefixTy == Mangler::Private)
123 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
124 else if (PrefixTy == Mangler::LinkerPrivate)
125 OutName.append(LinkerPrivatePrefix,
126 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
127 OutName.append(Prefix, Prefix+strlen(Prefix));
130 // Add the piece that we already scanned through.
131 OutName.append(X.begin(), I);
133 // Otherwise, construct the string the expensive way.
134 for (StringRef::iterator E = X.end(); I != E; ++I) {
136 const char *Quote = "_QQ_";
137 OutName.append(Quote, Quote+4);
138 } else if (*I == '\n') {
139 const char *Newline = "_NL_";
140 OutName.append(Newline, Newline+4);
142 OutName.push_back(*I);
145 // Add trailing quote.
146 OutName.push_back('"');
149 /// getMangledName - Returns the mangled name of V, an LLVM Value,
150 /// in the current module. If 'Suffix' is specified, the name ends with the
151 /// specified suffix. If 'ForcePrivate' is specified, the label is specified
152 /// to have a private label prefix.
154 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
156 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
157 "Intrinsic functions cannot be mangled by Mangler");
159 ManglerPrefixTy PrefixTy =
160 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
161 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
163 SmallString<128> Result;
165 makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy);
166 return Result.str().str();
169 // Get the ID for the global, assigning a new one if we haven't got one
171 unsigned &ID = AnonGlobalIDs[GV];
172 if (ID == 0) ID = NextAnonGlobalID++;
174 // Must mangle the global into a unique ID.
175 makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy);
176 return Result.str().str();
180 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
181 /// and the specified global variable's name. If the global variable doesn't
182 /// have a name, this fills in a unique name for the global.
183 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
184 const GlobalValue *GV,
185 bool isImplicitlyPrivate) {
187 // If the global is anonymous or not led with \1, then add the appropriate
189 if (!GV->hasName() || GV->getName()[0] != '\1') {
190 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
191 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
192 else if (GV->hasLinkerPrivateLinkage())
193 OutName.append(LinkerPrivatePrefix,
194 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
195 OutName.append(Prefix, Prefix+strlen(Prefix));
198 // If the global has a name, just append it now.
200 StringRef Name = GV->getName();
202 // Strip off the prefix marker if present.
204 OutName.append(Name.begin(), Name.end());
206 OutName.append(Name.begin()+1, Name.end());
210 // If the global variable doesn't have a name, return a unique name for the
211 // global based on a numbering.
213 // Get the ID for the global, assigning a new one if we haven't got one
215 unsigned &ID = AnonGlobalIDs[GV];
216 if (ID == 0) ID = NextAnonGlobalID++;
218 // Must mangle the global into a unique ID.
219 raw_svector_ostream(OutName) << "__unnamed_" << ID;
223 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
224 const char *linkerPrivatePrefix)
225 : Prefix(prefix), PrivatePrefix(privatePrefix),
226 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
227 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
228 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
230 // Letters and numbers are acceptable.
231 for (unsigned char X = 'a'; X <= 'z'; ++X)
232 markCharAcceptable(X);
233 for (unsigned char X = 'A'; X <= 'Z'; ++X)
234 markCharAcceptable(X);
235 for (unsigned char X = '0'; X <= '9'; ++X)
236 markCharAcceptable(X);
238 // These chars are acceptable.
239 markCharAcceptable('_');
240 markCharAcceptable('$');
241 markCharAcceptable('.');
242 markCharAcceptable('@');