blob: 5783ddfba94359bb1ee10b5d5a87381e03c9f6af [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
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 Lattnerb252cbb2010-01-13 05:02:57 +000035std::string Mangler::makeNameProper(const Twine &TheName,
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000036 ManglerPrefixTy PrefixTy) {
Chris Lattnerb252cbb2010-01-13 05:02:57 +000037 SmallString<256> TmpData;
38 TheName.toVector(TmpData);
39 StringRef X = TmpData.str();
Chris Lattnerf34815b2009-07-14 04:50:12 +000040 assert(!X.empty() && "Cannot mangle empty strings");
Chris Lattnercc9c0332005-09-24 08:24:28 +000041
Chris Lattner4b155fa2005-11-10 19:30:07 +000042 if (!UseQuotes) {
Evan Chengcfdbfcc2009-05-05 22:50:29 +000043 std::string Result;
44
Chris Lattner4b155fa2005-11-10 19:30:07 +000045 // If X does not start with (char)1, add the prefix.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000046 bool NeedPrefix = true;
Chris Lattner04a7ce82010-01-13 04:55:33 +000047 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000048 if (*I == 1) {
49 NeedPrefix = false;
Chris Lattner4b155fa2005-11-10 19:30:07 +000050 ++I; // Skip over the marker.
Evan Chengcfdbfcc2009-05-05 22:50:29 +000051 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000052
Anton Korobeynikov592638a2009-09-18 16:57:42 +000053 // Mangle the first letter specially, don't allow numbers unless the target
54 // explicitly allows them.
55 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
Chris Lattner4b155fa2005-11-10 19:30:07 +000056 Result += MangleLetter(*I++);
57
Chris Lattner04a7ce82010-01-13 04:55:33 +000058 for (StringRef::iterator E = X.end(); I != E; ++I) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000059 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000060 Result += MangleLetter(*I);
61 else
62 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000063 }
Chris Lattnerecc301d2005-11-10 23:24:26 +000064
Chris Lattner8c9a96b2009-07-14 18:17:16 +000065 if (NeedPrefix) {
66 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000067
Bill Wendling1bcfbff2009-07-20 19:41:27 +000068 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +000069 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +000070 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000071 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +000072 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000073
Evan Chengcfdbfcc2009-05-05 22:50:29 +000074 return Result;
Chris Lattner4b155fa2005-11-10 19:30:07 +000075 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000076
77 bool NeedPrefix = true;
78 bool NeedQuotes = false;
79 std::string Result;
Chris Lattner04a7ce82010-01-13 04:55:33 +000080 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000081 if (*I == 1) {
82 NeedPrefix = false;
83 ++I; // Skip over the marker.
84 }
85
86 // If the first character is a number, we need quotes.
87 if (*I >= '0' && *I <= '9')
88 NeedQuotes = true;
89
90 // Do an initial scan of the string, checking to see if we need quotes or
91 // to escape a '"' or not.
92 if (!NeedQuotes)
Chris Lattner04a7ce82010-01-13 04:55:33 +000093 for (StringRef::iterator E = X.end(); I != E; ++I)
Evan Chengcfdbfcc2009-05-05 22:50:29 +000094 if (!isCharAcceptable(*I)) {
95 NeedQuotes = true;
96 break;
97 }
98
99 // In the common case, we don't need quotes. Handle this quickly.
100 if (!NeedQuotes) {
Chris Lattnerf34815b2009-07-14 04:50:12 +0000101 if (!NeedPrefix)
102 return X.substr(1); // Strip off the \001.
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000103
Chris Lattner04a7ce82010-01-13 04:55:33 +0000104 Result = Prefix + X.str();
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000105
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000106 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000107 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000108 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000109 Result = LinkerPrivatePrefix + Result;
110
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000111 return Result;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000112 }
Devang Patelc6faffd2009-08-17 23:17:17 +0000113
114 if (NeedPrefix)
Chris Lattner04a7ce82010-01-13 04:55:33 +0000115 Result = X.substr(0, I-X.begin()).str();
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000116
117 // Otherwise, construct the string the expensive way.
Chris Lattner04a7ce82010-01-13 04:55:33 +0000118 for (StringRef::iterator E = X.end(); I != E; ++I) {
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000119 if (*I == '"')
120 Result += "_QQ_";
121 else if (*I == '\n')
122 Result += "_NL_";
123 else
124 Result += *I;
125 }
126
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000127 if (NeedPrefix) {
128 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000129
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000130 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000131 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000132 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000133 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000134 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000135
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000136 Result = '"' + Result + '"';
Chris Lattner9527fea2003-08-24 21:08:38 +0000137 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000138}
139
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000140/// getMangledName - Returns the mangled name of V, an LLVM Value,
141/// in the current module. If 'Suffix' is specified, the name ends with the
142/// specified suffix. If 'ForcePrivate' is specified, the label is specified
143/// to have a private label prefix.
144///
145std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
146 bool ForcePrivate) {
Chris Lattner05f19762009-07-14 00:15:14 +0000147 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
148 "Intrinsic functions cannot be mangled by Mangler");
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000149
150 ManglerPrefixTy PrefixTy =
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000151 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
152 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000153
Chris Lattnerf34815b2009-07-14 04:50:12 +0000154 if (GV->hasName())
Daniel Dunbar5899dda2009-07-22 21:33:09 +0000155 return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
Chris Lattner105efaf2009-07-14 00:01:06 +0000156
157 // Get the ID for the global, assigning a new one if we haven't got one
158 // already.
159 unsigned &ID = AnonGlobalIDs[GV];
160 if (ID == 0) ID = NextAnonGlobalID++;
161
162 // Must mangle the global into a unique ID.
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000163 return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
Brian Gaeked4dff192003-07-24 20:20:58 +0000164}
165
Chris Lattner840c8d72009-09-11 05:40:42 +0000166
167/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
168/// and the specified global variable's name. If the global variable doesn't
169/// have a name, this fills in a unique name for the global.
170void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
171 const GlobalValue *GV,
172 bool isImplicitlyPrivate) {
173
174 // If the global is anonymous or not led with \1, then add the appropriate
175 // prefix.
176 if (!GV->hasName() || GV->getName()[0] != '\1') {
Chris Lattner840c8d72009-09-11 05:40:42 +0000177 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
178 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
179 else if (GV->hasLinkerPrivateLinkage())
180 OutName.append(LinkerPrivatePrefix,
181 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
Chris Lattnerc827f532009-09-11 05:51:29 +0000182 OutName.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner840c8d72009-09-11 05:40:42 +0000183 }
184
185 // If the global has a name, just append it now.
186 if (GV->hasName()) {
187 StringRef Name = GV->getName();
188
189 // Strip off the prefix marker if present.
190 if (Name[0] != '\1')
191 OutName.append(Name.begin(), Name.end());
192 else
193 OutName.append(Name.begin()+1, Name.end());
194 return;
195 }
196
197 // If the global variable doesn't have a name, return a unique name for the
198 // global based on a numbering.
199
200 // Get the ID for the global, assigning a new one if we haven't got one
201 // already.
202 unsigned &ID = AnonGlobalIDs[GV];
203 if (ID == 0) ID = NextAnonGlobalID++;
204
205 // Must mangle the global into a unique ID.
206 raw_svector_ostream(OutName) << "__unnamed_" << ID;
207}
208
209
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000210Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
211 const char *linkerPrivatePrefix)
212 : Prefix(prefix), PrivatePrefix(privatePrefix),
213 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
Nuno Lopes2c7e72c2009-09-21 14:11:56 +0000214 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000215 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000216
217 // Letters and numbers are acceptable.
218 for (unsigned char X = 'a'; X <= 'z'; ++X)
219 markCharAcceptable(X);
220 for (unsigned char X = 'A'; X <= 'Z'; ++X)
221 markCharAcceptable(X);
222 for (unsigned char X = '0'; X <= '9'; ++X)
223 markCharAcceptable(X);
224
225 // These chars are acceptable.
226 markCharAcceptable('_');
227 markCharAcceptable('$');
228 markCharAcceptable('.');
Chris Lattner3d6c8eb2009-09-13 18:04:46 +0000229 markCharAcceptable('@');
Brian Gaeked4dff192003-07-24 20:20:58 +0000230}