blob: a5fbf264c62cfa77757a618cacd5b6a43ca00679 [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 Lattner840c8d72009-09-11 05:40:42 +000019#include "llvm/ADT/SmallVector.h"
20#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
27static std::string MangleLetter(unsigned char C) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000028 char Result[] = { '_', HexDigit(C >> 4), HexDigit(C & 15), '_', 0 };
29 return Result;
Chris Lattner9527fea2003-08-24 21:08:38 +000030}
31
32/// makeNameProper - We don't want identifier names non-C-identifier characters
33/// in them, so mangle them as appropriate.
Misha Brukmanb1c93172005-04-21 23:48:37 +000034///
Chris Lattnerf34815b2009-07-14 04:50:12 +000035std::string Mangler::makeNameProper(const std::string &X,
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000036 ManglerPrefixTy PrefixTy) {
Chris Lattnerf34815b2009-07-14 04:50:12 +000037 assert(!X.empty() && "Cannot mangle empty strings");
Chris Lattnercc9c0332005-09-24 08:24:28 +000038
Chris Lattner4b155fa2005-11-10 19:30:07 +000039 if (!UseQuotes) {
Evan Chengcfdbfcc2009-05-05 22:50:29 +000040 std::string Result;
41
Chris Lattner4b155fa2005-11-10 19:30:07 +000042 // If X does not start with (char)1, add the prefix.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000043 bool NeedPrefix = true;
Chris Lattner4b155fa2005-11-10 19:30:07 +000044 std::string::const_iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000045 if (*I == 1) {
46 NeedPrefix = false;
Chris Lattner4b155fa2005-11-10 19:30:07 +000047 ++I; // Skip over the marker.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000048 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000049
Anton Korobeynikov592638a2009-09-18 16:57:42 +000050 // Mangle the first letter specially, don't allow numbers unless the target
51 // explicitly allows them.
52 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
Chris Lattner4b155fa2005-11-10 19:30:07 +000053 Result += MangleLetter(*I++);
54
Chris Lattnere1d34bac2005-11-10 21:40:01 +000055 for (std::string::const_iterator E = X.end(); I != E; ++I) {
56 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000057 Result += MangleLetter(*I);
58 else
59 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000060 }
Chris Lattnerecc301d2005-11-10 23:24:26 +000061
Chris Lattner8c9a96b2009-07-14 18:17:16 +000062 if (NeedPrefix) {
63 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000064
Bill Wendling1bcfbff2009-07-20 19:41:27 +000065 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +000066 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +000067 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000068 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +000069 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000070
Evan Chengcfdbfcc2009-05-05 22:50:29 +000071 return Result;
Chris Lattner4b155fa2005-11-10 19:30:07 +000072 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000073
74 bool NeedPrefix = true;
75 bool NeedQuotes = false;
76 std::string Result;
77 std::string::const_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 (std::string::const_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) {
Chris Lattnerf34815b2009-07-14 04:50:12 +000098 if (!NeedPrefix)
99 return X.substr(1); // Strip off the \001.
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000100
101 Result = Prefix + X;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000102
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000103 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000104 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000105 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000106 Result = LinkerPrivatePrefix + Result;
107
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000108 return Result;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000109 }
Devang Patelc6faffd2009-08-17 23:17:17 +0000110
111 if (NeedPrefix)
112 Result = X.substr(0, I-X.begin());
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000113
114 // Otherwise, construct the string the expensive way.
115 for (std::string::const_iterator E = X.end(); I != E; ++I) {
116 if (*I == '"')
117 Result += "_QQ_";
118 else if (*I == '\n')
119 Result += "_NL_";
120 else
121 Result += *I;
122 }
123
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000124 if (NeedPrefix) {
125 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000126
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000127 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000128 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000129 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000130 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000131 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000132
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000133 Result = '"' + Result + '"';
Chris Lattner9527fea2003-08-24 21:08:38 +0000134 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000135}
136
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000137/// 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.
141///
142std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
143 bool ForcePrivate) {
Chris Lattner05f19762009-07-14 00:15:14 +0000144 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
145 "Intrinsic functions cannot be mangled by Mangler");
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000146
147 ManglerPrefixTy PrefixTy =
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000148 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
149 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000150
Chris Lattnerf34815b2009-07-14 04:50:12 +0000151 if (GV->hasName())
Daniel Dunbar5899dda2009-07-22 21:33:09 +0000152 return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
Chris Lattner105efaf2009-07-14 00:01:06 +0000153
154 // Get the ID for the global, assigning a new one if we haven't got one
155 // already.
156 unsigned &ID = AnonGlobalIDs[GV];
157 if (ID == 0) ID = NextAnonGlobalID++;
158
159 // Must mangle the global into a unique ID.
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000160 return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
Brian Gaeked4dff192003-07-24 20:20:58 +0000161}
162
Chris Lattner840c8d72009-09-11 05:40:42 +0000163
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.
167void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
168 const GlobalValue *GV,
169 bool isImplicitlyPrivate) {
170
171 // If the global is anonymous or not led with \1, then add the appropriate
172 // prefix.
173 if (!GV->hasName() || GV->getName()[0] != '\1') {
Chris Lattner840c8d72009-09-11 05:40:42 +0000174 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
175 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
176 else if (GV->hasLinkerPrivateLinkage())
177 OutName.append(LinkerPrivatePrefix,
178 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
Chris Lattnerc827f532009-09-11 05:51:29 +0000179 OutName.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner840c8d72009-09-11 05:40:42 +0000180 }
181
182 // If the global has a name, just append it now.
183 if (GV->hasName()) {
184 StringRef Name = GV->getName();
185
186 // Strip off the prefix marker if present.
187 if (Name[0] != '\1')
188 OutName.append(Name.begin(), Name.end());
189 else
190 OutName.append(Name.begin()+1, Name.end());
191 return;
192 }
193
194 // If the global variable doesn't have a name, return a unique name for the
195 // global based on a numbering.
196
197 // Get the ID for the global, assigning a new one if we haven't got one
198 // already.
199 unsigned &ID = AnonGlobalIDs[GV];
200 if (ID == 0) ID = NextAnonGlobalID++;
201
202 // Must mangle the global into a unique ID.
203 raw_svector_ostream(OutName) << "__unnamed_" << ID;
204}
205
206
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000207Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
208 const char *linkerPrivatePrefix)
209 : Prefix(prefix), PrivatePrefix(privatePrefix),
210 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
Chris Lattnerc35b5ba2009-07-15 04:50:47 +0000211 NextAnonGlobalID(1) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000212 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000213
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);
221
222 // These chars are acceptable.
223 markCharAcceptable('_');
224 markCharAcceptable('$');
225 markCharAcceptable('.');
Chris Lattner3d6c8eb2009-09-13 18:04:46 +0000226 markCharAcceptable('@');
Brian Gaeked4dff192003-07-24 20:20:58 +0000227}