blob: 69a24a0bc2847bccb22ac1c80191b40bf56a273a [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//
10// Unified name mangler for CWriter and assembly backends.
11//
12//===----------------------------------------------------------------------===//
13
Brian Gaekeb0078fa2003-07-25 20:21:20 +000014#include "llvm/Support/Mangler.h"
Chris Lattner840c8d72009-09-11 05:40:42 +000015#include "llvm/Function.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000017#include "llvm/ADT/StringExtras.h"
Evan Cheng0e697342008-07-10 00:04:23 +000018#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
Chris Lattner9527fea2003-08-24 21:08:38 +000023static char HexDigit(int V) {
24 return V < 10 ? V+'0' : V+'A'-10;
25}
26
Chris Lattner209aeca2010-01-13 06:38:18 +000027static 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('_');
Chris Lattner9527fea2003-08-24 21:08:38 +000032}
33
34/// makeNameProper - We don't want identifier names non-C-identifier characters
35/// in them, so mangle them as appropriate.
Misha Brukmanb1c93172005-04-21 23:48:37 +000036///
Chris Lattner33535b32010-01-13 07:01:09 +000037/// FIXME: This is deprecated, new code should use getNameWithPrefix and use
38/// MCSymbol printing to handle quotes or not etc.
39///
Chris Lattner209aeca2010-01-13 06:38:18 +000040void Mangler::makeNameProper(SmallVectorImpl<char> &OutName,
41 const Twine &TheName,
42 ManglerPrefixTy PrefixTy) {
Chris Lattnerb252cbb2010-01-13 05:02:57 +000043 SmallString<256> TmpData;
44 TheName.toVector(TmpData);
45 StringRef X = TmpData.str();
Chris Lattnerf34815b2009-07-14 04:50:12 +000046 assert(!X.empty() && "Cannot mangle empty strings");
Chris Lattnercc9c0332005-09-24 08:24:28 +000047
Chris Lattner4b155fa2005-11-10 19:30:07 +000048 if (!UseQuotes) {
49 // If X does not start with (char)1, add the prefix.
Chris Lattner04a7ce82010-01-13 04:55:33 +000050 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000051 if (*I == 1) {
Chris Lattner209aeca2010-01-13 06:38:18 +000052 ++I; // Skip over the no-prefix marker.
53 } else {
54 if (PrefixTy == Mangler::Private)
55 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
56 else if (PrefixTy == Mangler::LinkerPrivate)
57 OutName.append(LinkerPrivatePrefix,
58 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
59 OutName.append(Prefix, Prefix+strlen(Prefix));
Evan Chengcfdbfcc2009-05-05 22:50:29 +000060 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000061
Anton Korobeynikov592638a2009-09-18 16:57:42 +000062 // Mangle the first letter specially, don't allow numbers unless the target
63 // explicitly allows them.
64 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
Chris Lattner209aeca2010-01-13 06:38:18 +000065 MangleLetter(OutName, *I++);
Chris Lattner4b155fa2005-11-10 19:30:07 +000066
Chris Lattner04a7ce82010-01-13 04:55:33 +000067 for (StringRef::iterator E = X.end(); I != E; ++I) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000068 if (!isCharAcceptable(*I))
Chris Lattner209aeca2010-01-13 06:38:18 +000069 MangleLetter(OutName, *I);
Chris Lattner4b155fa2005-11-10 19:30:07 +000070 else
Chris Lattner209aeca2010-01-13 06:38:18 +000071 OutName.push_back(*I);
Chris Lattnere1d34bac2005-11-10 21:40:01 +000072 }
Chris Lattner209aeca2010-01-13 06:38:18 +000073 return;
Chris Lattner4b155fa2005-11-10 19:30:07 +000074 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000075
76 bool NeedPrefix = true;
77 bool NeedQuotes = false;
Chris Lattner04a7ce82010-01-13 04:55:33 +000078 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000079 if (*I == 1) {
80 NeedPrefix = false;
81 ++I; // Skip over the marker.
82 }
83
84 // If the first character is a number, we need quotes.
85 if (*I >= '0' && *I <= '9')
86 NeedQuotes = true;
87
88 // Do an initial scan of the string, checking to see if we need quotes or
89 // to escape a '"' or not.
90 if (!NeedQuotes)
Chris Lattner04a7ce82010-01-13 04:55:33 +000091 for (StringRef::iterator E = X.end(); I != E; ++I)
Evan Chengcfdbfcc2009-05-05 22:50:29 +000092 if (!isCharAcceptable(*I)) {
93 NeedQuotes = true;
94 break;
95 }
96
97 // In the common case, we don't need quotes. Handle this quickly.
98 if (!NeedQuotes) {
Chris Lattner209aeca2010-01-13 06:38:18 +000099 if (!NeedPrefix) {
100 OutName.append(X.begin()+1, X.end()); // Strip off the \001.
101 return;
102 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000103
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000104 if (PrefixTy == Mangler::Private)
Chris Lattner209aeca2010-01-13 06:38:18 +0000105 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000106 else if (PrefixTy == Mangler::LinkerPrivate)
Chris Lattner209aeca2010-01-13 06:38:18 +0000107 OutName.append(LinkerPrivatePrefix,
108 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
109
110 if (Prefix[0] == 0)
111 ; // Common noop, no prefix.
112 else if (Prefix[1] == 0)
113 OutName.push_back(Prefix[0]); // Common, one character prefix.
114 else
115 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
116 OutName.append(X.begin(), X.end());
117 return;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000118 }
Devang Patelc6faffd2009-08-17 23:17:17 +0000119
Chris Lattner209aeca2010-01-13 06:38:18 +0000120 // Add leading quote.
121 OutName.push_back('"');
122
123 // Add prefixes unless disabled.
124 if (NeedPrefix) {
125 if (PrefixTy == Mangler::Private)
126 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
127 else if (PrefixTy == Mangler::LinkerPrivate)
128 OutName.append(LinkerPrivatePrefix,
129 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
130 OutName.append(Prefix, Prefix+strlen(Prefix));
131 }
132
133 // Add the piece that we already scanned through.
Chris Lattner98b05e02010-01-13 07:50:21 +0000134 OutName.append(X.begin()+!NeedPrefix, I);
Chris Lattner209aeca2010-01-13 06:38:18 +0000135
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000136 // Otherwise, construct the string the expensive way.
Chris Lattner04a7ce82010-01-13 04:55:33 +0000137 for (StringRef::iterator E = X.end(); I != E; ++I) {
Chris Lattner209aeca2010-01-13 06:38:18 +0000138 if (*I == '"') {
139 const char *Quote = "_QQ_";
140 OutName.append(Quote, Quote+4);
141 } else if (*I == '\n') {
142 const char *Newline = "_NL_";
143 OutName.append(Newline, Newline+4);
144 } else
145 OutName.push_back(*I);
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000146 }
147
Chris Lattner209aeca2010-01-13 06:38:18 +0000148 // Add trailing quote.
149 OutName.push_back('"');
Brian Gaeked4dff192003-07-24 20:20:58 +0000150}
151
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000152/// getMangledName - Returns the mangled name of V, an LLVM Value,
153/// in the current module. If 'Suffix' is specified, the name ends with the
154/// specified suffix. If 'ForcePrivate' is specified, the label is specified
155/// to have a private label prefix.
156///
Chris Lattner33535b32010-01-13 07:01:09 +0000157/// FIXME: This is deprecated, new code should use getNameWithPrefix and use
158/// MCSymbol printing to handle quotes or not etc.
159///
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000160std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
161 bool ForcePrivate) {
Chris Lattner05f19762009-07-14 00:15:14 +0000162 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
163 "Intrinsic functions cannot be mangled by Mangler");
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000164
165 ManglerPrefixTy PrefixTy =
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000166 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
167 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000168
Chris Lattner209aeca2010-01-13 06:38:18 +0000169 SmallString<128> Result;
170 if (GV->hasName()) {
171 makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy);
172 return Result.str().str();
173 }
Chris Lattner105efaf2009-07-14 00:01:06 +0000174
175 // Get the ID for the global, assigning a new one if we haven't got one
176 // already.
177 unsigned &ID = AnonGlobalIDs[GV];
178 if (ID == 0) ID = NextAnonGlobalID++;
179
180 // Must mangle the global into a unique ID.
Chris Lattner209aeca2010-01-13 06:38:18 +0000181 makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy);
182 return Result.str().str();
Brian Gaeked4dff192003-07-24 20:20:58 +0000183}
184
Chris Lattner33535b32010-01-13 07:01:09 +0000185/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
186/// and the specified name as the global variable name. GVName must not be
187/// empty.
188void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
189 const Twine &GVName, ManglerPrefixTy PrefixTy) {
190 SmallString<256> TmpData;
Chris Lattner11265342010-01-13 07:12:06 +0000191 StringRef Name;
192 if (GVName.isSingleStringRef())
193 Name = GVName.getSingleStringRef();
194 else {
195 GVName.toVector(TmpData);
196 Name = TmpData.str();
197 }
Chris Lattner33535b32010-01-13 07:01:09 +0000198 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
199
200 // If the global name is not led with \1, add the appropriate prefixes.
201 if (Name[0] != '\1') {
202 if (PrefixTy == Mangler::Private)
203 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
204 else if (PrefixTy == Mangler::LinkerPrivate)
205 OutName.append(LinkerPrivatePrefix,
206 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
207
208 if (Prefix[0] == 0)
209 ; // Common noop, no prefix.
210 else if (Prefix[1] == 0)
211 OutName.push_back(Prefix[0]); // Common, one character prefix.
212 else
213 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
214 } else {
215 Name = Name.substr(1);
216 }
217
218 OutName.append(Name.begin(), Name.end());
219}
220
Chris Lattner840c8d72009-09-11 05:40:42 +0000221
222/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
223/// and the specified global variable's name. If the global variable doesn't
224/// have a name, this fills in a unique name for the global.
225void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
226 const GlobalValue *GV,
227 bool isImplicitlyPrivate) {
Chris Lattner33535b32010-01-13 07:01:09 +0000228 // If this global has a name, handle it simply.
Chris Lattner840c8d72009-09-11 05:40:42 +0000229 if (GV->hasName()) {
Chris Lattner33535b32010-01-13 07:01:09 +0000230 ManglerPrefixTy PrefixTy = Mangler::Default;
231 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
232 PrefixTy = Mangler::Private;
233 else if (GV->hasLinkerPrivateLinkage())
234 PrefixTy = Mangler::LinkerPrivate;
Chris Lattner840c8d72009-09-11 05:40:42 +0000235
Chris Lattner33535b32010-01-13 07:01:09 +0000236 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
Chris Lattner840c8d72009-09-11 05:40:42 +0000237 }
238
239 // If the global variable doesn't have a name, return a unique name for the
240 // global based on a numbering.
241
Chris Lattner33535b32010-01-13 07:01:09 +0000242 // Anonymous names always get prefixes.
243 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
244 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
245 else if (GV->hasLinkerPrivateLinkage())
246 OutName.append(LinkerPrivatePrefix,
247 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
248 OutName.append(Prefix, Prefix+strlen(Prefix));
249
Chris Lattner840c8d72009-09-11 05:40:42 +0000250 // Get the ID for the global, assigning a new one if we haven't got one
251 // already.
252 unsigned &ID = AnonGlobalIDs[GV];
253 if (ID == 0) ID = NextAnonGlobalID++;
254
255 // Must mangle the global into a unique ID.
256 raw_svector_ostream(OutName) << "__unnamed_" << ID;
257}
258
259
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000260Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
261 const char *linkerPrivatePrefix)
262 : Prefix(prefix), PrivatePrefix(privatePrefix),
263 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
Nuno Lopes2c7e72c2009-09-21 14:11:56 +0000264 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000265 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000266
267 // Letters and numbers are acceptable.
268 for (unsigned char X = 'a'; X <= 'z'; ++X)
269 markCharAcceptable(X);
270 for (unsigned char X = 'A'; X <= 'Z'; ++X)
271 markCharAcceptable(X);
272 for (unsigned char X = '0'; X <= '9'; ++X)
273 markCharAcceptable(X);
274
275 // These chars are acceptable.
276 markCharAcceptable('_');
277 markCharAcceptable('$');
278 markCharAcceptable('.');
Chris Lattner3d6c8eb2009-09-13 18:04:46 +0000279 markCharAcceptable('@');
Brian Gaeked4dff192003-07-24 20:20:58 +0000280}