blob: 7d9f330f4c4e1e9174bb43a488cc6b10c2f813b5 [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;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000044 StringRef X = TheName.toStringRef(TmpData);
Chris Lattnerf34815b2009-07-14 04:50:12 +000045 assert(!X.empty() && "Cannot mangle empty strings");
Chris Lattnercc9c0332005-09-24 08:24:28 +000046
Chris Lattner4b155fa2005-11-10 19:30:07 +000047 if (!UseQuotes) {
48 // If X does not start with (char)1, add the prefix.
Chris Lattner04a7ce82010-01-13 04:55:33 +000049 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000050 if (*I == 1) {
Chris Lattner209aeca2010-01-13 06:38:18 +000051 ++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));
Evan Chengcfdbfcc2009-05-05 22:50:29 +000059 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000060
Anton Korobeynikov592638a2009-09-18 16:57:42 +000061 // Mangle the first letter specially, don't allow numbers unless the target
62 // explicitly allows them.
63 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
Chris Lattner209aeca2010-01-13 06:38:18 +000064 MangleLetter(OutName, *I++);
Chris Lattner4b155fa2005-11-10 19:30:07 +000065
Chris Lattner04a7ce82010-01-13 04:55:33 +000066 for (StringRef::iterator E = X.end(); I != E; ++I) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000067 if (!isCharAcceptable(*I))
Chris Lattner209aeca2010-01-13 06:38:18 +000068 MangleLetter(OutName, *I);
Chris Lattner4b155fa2005-11-10 19:30:07 +000069 else
Chris Lattner209aeca2010-01-13 06:38:18 +000070 OutName.push_back(*I);
Chris Lattnere1d34bac2005-11-10 21:40:01 +000071 }
Chris Lattner209aeca2010-01-13 06:38:18 +000072 return;
Chris Lattner4b155fa2005-11-10 19:30:07 +000073 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000074
75 bool NeedPrefix = true;
76 bool NeedQuotes = false;
Chris Lattner04a7ce82010-01-13 04:55:33 +000077 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000078 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)
Chris Lattner04a7ce82010-01-13 04:55:33 +000090 for (StringRef::iterator E = X.end(); I != E; ++I)
Evan Chengcfdbfcc2009-05-05 22:50:29 +000091 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) {
Chris Lattner209aeca2010-01-13 06:38:18 +000098 if (!NeedPrefix) {
99 OutName.append(X.begin()+1, X.end()); // Strip off the \001.
100 return;
101 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000102
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000103 if (PrefixTy == Mangler::Private)
Chris Lattner209aeca2010-01-13 06:38:18 +0000104 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000105 else if (PrefixTy == Mangler::LinkerPrivate)
Chris Lattner209aeca2010-01-13 06:38:18 +0000106 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;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000117 }
Devang Patelc6faffd2009-08-17 23:17:17 +0000118
Chris Lattner209aeca2010-01-13 06:38:18 +0000119 // 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.
Chris Lattner98b05e02010-01-13 07:50:21 +0000133 OutName.append(X.begin()+!NeedPrefix, I);
Chris Lattner209aeca2010-01-13 06:38:18 +0000134
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000135 // Otherwise, construct the string the expensive way.
Chris Lattner04a7ce82010-01-13 04:55:33 +0000136 for (StringRef::iterator E = X.end(); I != E; ++I) {
Chris Lattner209aeca2010-01-13 06:38:18 +0000137 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);
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000145 }
146
Chris Lattner209aeca2010-01-13 06:38:18 +0000147 // Add trailing quote.
148 OutName.push_back('"');
Brian Gaeked4dff192003-07-24 20:20:58 +0000149}
150
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000151/// 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///
Chris Lattner33535b32010-01-13 07:01:09 +0000156/// FIXME: This is deprecated, new code should use getNameWithPrefix and use
157/// MCSymbol printing to handle quotes or not etc.
158///
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000159std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
160 bool ForcePrivate) {
Chris Lattner05f19762009-07-14 00:15:14 +0000161 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
162 "Intrinsic functions cannot be mangled by Mangler");
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000163
164 ManglerPrefixTy PrefixTy =
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000165 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
166 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000167
Chris Lattner209aeca2010-01-13 06:38:18 +0000168 SmallString<128> Result;
169 if (GV->hasName()) {
170 makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy);
171 return Result.str().str();
172 }
Chris Lattner105efaf2009-07-14 00:01:06 +0000173
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.
Chris Lattner209aeca2010-01-13 06:38:18 +0000180 makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy);
181 return Result.str().str();
Brian Gaeked4dff192003-07-24 20:20:58 +0000182}
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
252
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000253Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
254 const char *linkerPrivatePrefix)
255 : Prefix(prefix), PrivatePrefix(privatePrefix),
256 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
Nuno Lopes2c7e72c2009-09-21 14:11:56 +0000257 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000258 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000259
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);
267
268 // These chars are acceptable.
269 markCharAcceptable('_');
270 markCharAcceptable('$');
271 markCharAcceptable('.');
Chris Lattner3d6c8eb2009-09-13 18:04:46 +0000272 markCharAcceptable('@');
Brian Gaeked4dff192003-07-24 20:20:58 +0000273}