blob: 9f6c9605076757a6659304206f5fa735c7d88a4c [file] [log] [blame]
Brian Gaeked4dff192003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeked4dff192003-07-24 20:20:58 +00009//
Rafael Espindola9794fdf2010-01-16 20:27:59 +000010// Unified name mangler for CWriter and assembly backends.
Brian Gaeked4dff192003-07-24 20:20:58 +000011//
12//===----------------------------------------------------------------------===//
13
Brian Gaekeb0078fa2003-07-25 20:21:20 +000014#include "llvm/Support/Mangler.h"
Rafael Espindola9794fdf2010-01-16 20:27:59 +000015#include "llvm/Function.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/StringMap.h"
Chris Lattnerb252cbb2010-01-13 05:02:57 +000019#include "llvm/ADT/SmallString.h"
Chris Lattner840c8d72009-09-11 05:40:42 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Rafael Espindola9794fdf2010-01-16 20:27:59 +000023static char HexDigit(int V) {
24 return V < 10 ? V+'0' : V+'A'-10;
25}
26
27static 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('_');
32}
33
34/// makeNameProper - We don't want identifier names non-C-identifier characters
35/// in them, so mangle them as appropriate.
36///
37/// FIXME: This is deprecated, new code should use getNameWithPrefix and use
38/// MCSymbol printing to handle quotes or not etc.
39///
40void Mangler::makeNameProper(SmallVectorImpl<char> &OutName,
41 const Twine &TheName,
42 ManglerPrefixTy PrefixTy) {
43 SmallString<256> TmpData;
44 StringRef X = TheName.toStringRef(TmpData);
45 assert(!X.empty() && "Cannot mangle empty strings");
46
47 if (!UseQuotes) {
48 // If X does not start with (char)1, add the prefix.
49 StringRef::iterator I = X.begin();
50 if (*I == 1) {
51 ++I; // Skip over the no-prefix marker.
52 } else {
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));
59 }
60
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++);
65
66 for (StringRef::iterator E = X.end(); I != E; ++I) {
67 if (!isCharAcceptable(*I))
68 MangleLetter(OutName, *I);
69 else
70 OutName.push_back(*I);
71 }
72 return;
73 }
74
75 bool NeedPrefix = true;
76 bool NeedQuotes = false;
77 StringRef::iterator I = X.begin();
78 if (*I == 1) {
79 NeedPrefix = false;
80 ++I; // Skip over the marker.
81 }
82
83 // If the first character is a number, we need quotes.
84 if (*I >= '0' && *I <= '9')
85 NeedQuotes = true;
86
87 // Do an initial scan of the string, checking to see if we need quotes or
88 // to escape a '"' or not.
89 if (!NeedQuotes)
90 for (StringRef::iterator E = X.end(); I != E; ++I)
91 if (!isCharAcceptable(*I)) {
92 NeedQuotes = true;
93 break;
94 }
95
96 // In the common case, we don't need quotes. Handle this quickly.
97 if (!NeedQuotes) {
98 if (!NeedPrefix) {
99 OutName.append(X.begin()+1, X.end()); // Strip off the \001.
100 return;
101 }
102
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));
108
109 if (Prefix[0] == 0)
110 ; // Common noop, no prefix.
111 else if (Prefix[1] == 0)
112 OutName.push_back(Prefix[0]); // Common, one character prefix.
113 else
114 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
115 OutName.append(X.begin(), X.end());
116 return;
117 }
118
119 // Add leading quote.
120 OutName.push_back('"');
121
122 // Add prefixes unless disabled.
123 if (NeedPrefix) {
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));
130 }
131
132 // Add the piece that we already scanned through.
133 OutName.append(X.begin()+!NeedPrefix, I);
134
135 // Otherwise, construct the string the expensive way.
136 for (StringRef::iterator E = X.end(); I != E; ++I) {
137 if (*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);
143 } else
144 OutName.push_back(*I);
145 }
146
147 // Add trailing quote.
148 OutName.push_back('"');
149}
150
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.
155///
156/// FIXME: This is deprecated, new code should use getNameWithPrefix and use
157/// MCSymbol printing to handle quotes or not etc.
158///
159std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
160 bool ForcePrivate) {
161 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
162 "Intrinsic functions cannot be mangled by Mangler");
163
164 ManglerPrefixTy PrefixTy =
165 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
166 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
167
168 SmallString<128> Result;
169 if (GV->hasName()) {
170 makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy);
171 return Result.str().str();
172 }
173
174 // Get the ID for the global, assigning a new one if we haven't got one
175 // already.
176 unsigned &ID = AnonGlobalIDs[GV];
177 if (ID == 0) ID = NextAnonGlobalID++;
178
179 // Must mangle the global into a unique ID.
180 makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy);
181 return Result.str().str();
182}
183
Chris Lattner33535b32010-01-13 07:01:09 +0000184/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
185/// and the specified name as the global variable name. GVName must not be
186/// empty.
187void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
188 const Twine &GVName, ManglerPrefixTy PrefixTy) {
189 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000190 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +0000191 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
192
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));
200
201 if (Prefix[0] == 0)
202 ; // Common noop, no prefix.
203 else if (Prefix[1] == 0)
204 OutName.push_back(Prefix[0]); // Common, one character prefix.
205 else
206 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
207 } else {
208 Name = Name.substr(1);
209 }
210
211 OutName.append(Name.begin(), Name.end());
212}
213
Chris Lattner840c8d72009-09-11 05:40:42 +0000214
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.
218void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
219 const GlobalValue *GV,
220 bool isImplicitlyPrivate) {
Chris Lattner33535b32010-01-13 07:01:09 +0000221 // If this global has a name, handle it simply.
Chris Lattner840c8d72009-09-11 05:40:42 +0000222 if (GV->hasName()) {
Chris Lattner33535b32010-01-13 07:01:09 +0000223 ManglerPrefixTy PrefixTy = Mangler::Default;
224 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
225 PrefixTy = Mangler::Private;
226 else if (GV->hasLinkerPrivateLinkage())
227 PrefixTy = Mangler::LinkerPrivate;
Chris Lattner840c8d72009-09-11 05:40:42 +0000228
Chris Lattner33535b32010-01-13 07:01:09 +0000229 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
Chris Lattner840c8d72009-09-11 05:40:42 +0000230 }
231
232 // If the global variable doesn't have a name, return a unique name for the
233 // global based on a numbering.
234
Chris Lattner33535b32010-01-13 07:01:09 +0000235 // 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));
242
Chris Lattner840c8d72009-09-11 05:40:42 +0000243 // Get the ID for the global, assigning a new one if we haven't got one
244 // already.
245 unsigned &ID = AnonGlobalIDs[GV];
246 if (ID == 0) ID = NextAnonGlobalID++;
247
248 // Must mangle the global into a unique ID.
249 raw_svector_ostream(OutName) << "__unnamed_" << ID;
250}
251
Chris Lattnerd3a21632010-01-16 18:06:34 +0000252/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
253/// and the specified global variable's name. If the global variable doesn't
254/// have a name, this fills in a unique name for the global.
255std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
256 bool isImplicitlyPrivate) {
257 SmallString<64> Buf;
258 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
259 return std::string(Buf.begin(), Buf.end());
260}
261
Chris Lattner840c8d72009-09-11 05:40:42 +0000262
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000263Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
264 const char *linkerPrivatePrefix)
265 : Prefix(prefix), PrivatePrefix(privatePrefix),
Rafael Espindola9794fdf2010-01-16 20:27:59 +0000266 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
267 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
268 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
269
270 // Letters and numbers are acceptable.
271 for (unsigned char X = 'a'; X <= 'z'; ++X)
272 markCharAcceptable(X);
273 for (unsigned char X = 'A'; X <= 'Z'; ++X)
274 markCharAcceptable(X);
275 for (unsigned char X = '0'; X <= '9'; ++X)
276 markCharAcceptable(X);
277
278 // These chars are acceptable.
279 markCharAcceptable('_');
280 markCharAcceptable('$');
281 markCharAcceptable('.');
282 markCharAcceptable('@');
Brian Gaeked4dff192003-07-24 20:20:58 +0000283}