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 /// FIXME: This is deprecated, new code should use getNameWithPrefix and use
38 /// MCSymbol printing to handle quotes or not etc.
40 void Mangler::makeNameProper(SmallVectorImpl<char> &OutName,
42 ManglerPrefixTy PrefixTy) {
43 SmallString<256> TmpData;
44 StringRef X = TheName.toStringRef(TmpData);
45 assert(!X.empty() && "Cannot mangle empty strings");
48 // If X does not start with (char)1, add the prefix.
49 StringRef::iterator I = X.begin();
51 ++I; // Skip over the no-prefix marker.
53 if (PrefixTy == Mangler::Private)
54 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
55 else if (PrefixTy == Mangler::LinkerPrivate)
56 OutName.append(LinkerPrivatePrefix,
57 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
58 OutName.append(Prefix, Prefix+strlen(Prefix));
61 // Mangle the first letter specially, don't allow numbers unless the target
62 // explicitly allows them.
63 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
64 MangleLetter(OutName, *I++);
66 for (StringRef::iterator E = X.end(); I != E; ++I) {
67 if (!isCharAcceptable(*I))
68 MangleLetter(OutName, *I);
70 OutName.push_back(*I);
75 bool NeedPrefix = true;
76 bool NeedQuotes = false;
77 StringRef::iterator I = X.begin();
80 ++I; // Skip over the marker.
83 // If the first character is a number, we need quotes.
84 if (*I >= '0' && *I <= '9')
87 // Do an initial scan of the string, checking to see if we need quotes or
88 // to escape a '"' or not.
90 for (StringRef::iterator E = X.end(); I != E; ++I)
91 if (!isCharAcceptable(*I)) {
96 // In the common case, we don't need quotes. Handle this quickly.
99 OutName.append(X.begin()+1, X.end()); // Strip off the \001.
103 if (PrefixTy == Mangler::Private)
104 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
105 else if (PrefixTy == Mangler::LinkerPrivate)
106 OutName.append(LinkerPrivatePrefix,
107 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
110 ; // Common noop, no prefix.
111 else if (Prefix[1] == 0)
112 OutName.push_back(Prefix[0]); // Common, one character prefix.
114 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
115 OutName.append(X.begin(), X.end());
119 // Add leading quote.
120 OutName.push_back('"');
122 // Add prefixes unless disabled.
124 if (PrefixTy == Mangler::Private)
125 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
126 else if (PrefixTy == Mangler::LinkerPrivate)
127 OutName.append(LinkerPrivatePrefix,
128 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
129 OutName.append(Prefix, Prefix+strlen(Prefix));
132 // Add the piece that we already scanned through.
133 OutName.append(X.begin()+!NeedPrefix, I);
135 // Otherwise, construct the string the expensive way.
136 for (StringRef::iterator E = X.end(); I != E; ++I) {
138 const char *Quote = "_QQ_";
139 OutName.append(Quote, Quote+4);
140 } else if (*I == '\n') {
141 const char *Newline = "_NL_";
142 OutName.append(Newline, Newline+4);
144 OutName.push_back(*I);
147 // Add trailing quote.
148 OutName.push_back('"');
151 /// getMangledName - Returns the mangled name of V, an LLVM Value,
152 /// in the current module. If 'Suffix' is specified, the name ends with the
153 /// specified suffix. If 'ForcePrivate' is specified, the label is specified
154 /// to have a private label prefix.
156 /// FIXME: This is deprecated, new code should use getNameWithPrefix and use
157 /// MCSymbol printing to handle quotes or not etc.
159 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
161 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
162 "Intrinsic functions cannot be mangled by Mangler");
164 ManglerPrefixTy PrefixTy =
165 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
166 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
168 SmallString<128> Result;
170 makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy);
171 return Result.str().str();
174 // Get the ID for the global, assigning a new one if we haven't got one
176 unsigned &ID = AnonGlobalIDs[GV];
177 if (ID == 0) ID = NextAnonGlobalID++;
179 // Must mangle the global into a unique ID.
180 makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy);
181 return Result.str().str();
184 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
185 /// and the specified name as the global variable name. GVName must not be
187 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
188 const Twine &GVName, ManglerPrefixTy PrefixTy) {
189 SmallString<256> TmpData;
190 StringRef Name = GVName.toStringRef(TmpData);
191 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
193 // If the global name is not led with \1, add the appropriate prefixes.
194 if (Name[0] != '\1') {
195 if (PrefixTy == Mangler::Private)
196 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
197 else if (PrefixTy == Mangler::LinkerPrivate)
198 OutName.append(LinkerPrivatePrefix,
199 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
202 ; // Common noop, no prefix.
203 else if (Prefix[1] == 0)
204 OutName.push_back(Prefix[0]); // Common, one character prefix.
206 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
208 Name = Name.substr(1);
211 OutName.append(Name.begin(), Name.end());
215 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
216 /// and the specified global variable's name. If the global variable doesn't
217 /// have a name, this fills in a unique name for the global.
218 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
219 const GlobalValue *GV,
220 bool isImplicitlyPrivate) {
221 // If this global has a name, handle it simply.
223 ManglerPrefixTy PrefixTy = Mangler::Default;
224 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
225 PrefixTy = Mangler::Private;
226 else if (GV->hasLinkerPrivateLinkage())
227 PrefixTy = Mangler::LinkerPrivate;
229 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
232 // If the global variable doesn't have a name, return a unique name for the
233 // global based on a numbering.
235 // Anonymous names always get prefixes.
236 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
237 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
238 else if (GV->hasLinkerPrivateLinkage())
239 OutName.append(LinkerPrivatePrefix,
240 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
241 OutName.append(Prefix, Prefix+strlen(Prefix));
243 // Get the ID for the global, assigning a new one if we haven't got one
245 unsigned &ID = AnonGlobalIDs[GV];
246 if (ID == 0) ID = NextAnonGlobalID++;
248 // Must mangle the global into a unique ID.
249 raw_svector_ostream(OutName) << "__unnamed_" << ID;
253 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
254 const char *linkerPrivatePrefix)
255 : Prefix(prefix), PrivatePrefix(privatePrefix),
256 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
257 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
258 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
260 // Letters and numbers are acceptable.
261 for (unsigned char X = 'a'; X <= 'z'; ++X)
262 markCharAcceptable(X);
263 for (unsigned char X = 'A'; X <= 'Z'; ++X)
264 markCharAcceptable(X);
265 for (unsigned char X = '0'; X <= '9'; ++X)
266 markCharAcceptable(X);
268 // These chars are acceptable.
269 markCharAcceptable('_');
270 markCharAcceptable('$');
271 markCharAcceptable('.');
272 markCharAcceptable('@');