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/SmallVector.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 std::string MangleLetter(unsigned char C) {
28 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
32 /// makeNameProper - We don't want identifier names non-C-identifier characters
33 /// in them, so mangle them as appropriate.
35 std::string Mangler::makeNameProper(const std::string &X,
36 ManglerPrefixTy PrefixTy) {
37 assert(!X.empty() && "Cannot mangle empty strings");
42 // If X does not start with (char)1, add the prefix.
43 bool NeedPrefix = true;
44 std::string::const_iterator I = X.begin();
47 ++I; // Skip over the marker.
50 // Mangle the first letter specially, don't allow numbers unless the target
51 // explicitly allows them.
52 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
53 Result += MangleLetter(*I++);
55 for (std::string::const_iterator E = X.end(); I != E; ++I) {
56 if (!isCharAcceptable(*I))
57 Result += MangleLetter(*I);
63 Result = Prefix + Result;
65 if (PrefixTy == Mangler::Private)
66 Result = PrivatePrefix + Result;
67 else if (PrefixTy == Mangler::LinkerPrivate)
68 Result = LinkerPrivatePrefix + Result;
74 bool NeedPrefix = true;
75 bool NeedQuotes = false;
77 std::string::const_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 (std::string::const_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 return X.substr(1); // Strip off the \001.
103 if (PrefixTy == Mangler::Private)
104 Result = PrivatePrefix + Result;
105 else if (PrefixTy == Mangler::LinkerPrivate)
106 Result = LinkerPrivatePrefix + Result;
112 Result = X.substr(0, I-X.begin());
114 // Otherwise, construct the string the expensive way.
115 for (std::string::const_iterator E = X.end(); I != E; ++I) {
125 Result = Prefix + Result;
127 if (PrefixTy == Mangler::Private)
128 Result = PrivatePrefix + Result;
129 else if (PrefixTy == Mangler::LinkerPrivate)
130 Result = LinkerPrivatePrefix + Result;
133 Result = '"' + Result + '"';
137 /// getMangledName - Returns the mangled name of V, an LLVM Value,
138 /// in the current module. If 'Suffix' is specified, the name ends with the
139 /// specified suffix. If 'ForcePrivate' is specified, the label is specified
140 /// to have a private label prefix.
142 std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
144 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
145 "Intrinsic functions cannot be mangled by Mangler");
147 ManglerPrefixTy PrefixTy =
148 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
149 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
152 return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
154 // Get the ID for the global, assigning a new one if we haven't got one
156 unsigned &ID = AnonGlobalIDs[GV];
157 if (ID == 0) ID = NextAnonGlobalID++;
159 // Must mangle the global into a unique ID.
160 return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
164 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
165 /// and the specified global variable's name. If the global variable doesn't
166 /// have a name, this fills in a unique name for the global.
167 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
168 const GlobalValue *GV,
169 bool isImplicitlyPrivate) {
171 // If the global is anonymous or not led with \1, then add the appropriate
173 if (!GV->hasName() || GV->getName()[0] != '\1') {
174 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
175 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
176 else if (GV->hasLinkerPrivateLinkage())
177 OutName.append(LinkerPrivatePrefix,
178 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
179 OutName.append(Prefix, Prefix+strlen(Prefix));
182 // If the global has a name, just append it now.
184 StringRef Name = GV->getName();
186 // Strip off the prefix marker if present.
188 OutName.append(Name.begin(), Name.end());
190 OutName.append(Name.begin()+1, Name.end());
194 // If the global variable doesn't have a name, return a unique name for the
195 // global based on a numbering.
197 // Get the ID for the global, assigning a new one if we haven't got one
199 unsigned &ID = AnonGlobalIDs[GV];
200 if (ID == 0) ID = NextAnonGlobalID++;
202 // Must mangle the global into a unique ID.
203 raw_svector_ostream(OutName) << "__unnamed_" << ID;
207 Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
208 const char *linkerPrivatePrefix)
209 : Prefix(prefix), PrivatePrefix(privatePrefix),
210 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
211 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
212 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
214 // Letters and numbers are acceptable.
215 for (unsigned char X = 'a'; X <= 'z'; ++X)
216 markCharAcceptable(X);
217 for (unsigned char X = 'A'; X <= 'Z'; ++X)
218 markCharAcceptable(X);
219 for (unsigned char X = '0'; X <= '9'; ++X)
220 markCharAcceptable(X);
222 // These chars are acceptable.
223 markCharAcceptable('_');
224 markCharAcceptable('$');
225 markCharAcceptable('.');
226 markCharAcceptable('@');