blob: da95fb93e02d1a1f6e402b6ba080b4653febb3ac [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 Lattner209aeca2010-01-13 06:38:18 +000037void Mangler::makeNameProper(SmallVectorImpl<char> &OutName,
38 const Twine &TheName,
39 ManglerPrefixTy PrefixTy) {
Chris Lattnerb252cbb2010-01-13 05:02:57 +000040 SmallString<256> TmpData;
41 TheName.toVector(TmpData);
42 StringRef X = TmpData.str();
Chris Lattnerf34815b2009-07-14 04:50:12 +000043 assert(!X.empty() && "Cannot mangle empty strings");
Chris Lattnercc9c0332005-09-24 08:24:28 +000044
Chris Lattner4b155fa2005-11-10 19:30:07 +000045 if (!UseQuotes) {
46 // If X does not start with (char)1, add the prefix.
Chris Lattner04a7ce82010-01-13 04:55:33 +000047 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000048 if (*I == 1) {
Chris Lattner209aeca2010-01-13 06:38:18 +000049 ++I; // Skip over the no-prefix marker.
50 } else {
51 if (PrefixTy == Mangler::Private)
52 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
53 else if (PrefixTy == Mangler::LinkerPrivate)
54 OutName.append(LinkerPrivatePrefix,
55 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
56 OutName.append(Prefix, Prefix+strlen(Prefix));
Evan Chengcfdbfcc2009-05-05 22:50:29 +000057 }
Chris Lattner4b155fa2005-11-10 19:30:07 +000058
Anton Korobeynikov592638a2009-09-18 16:57:42 +000059 // Mangle the first letter specially, don't allow numbers unless the target
60 // explicitly allows them.
61 if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9')
Chris Lattner209aeca2010-01-13 06:38:18 +000062 MangleLetter(OutName, *I++);
Chris Lattner4b155fa2005-11-10 19:30:07 +000063
Chris Lattner04a7ce82010-01-13 04:55:33 +000064 for (StringRef::iterator E = X.end(); I != E; ++I) {
Chris Lattnere1d34bac2005-11-10 21:40:01 +000065 if (!isCharAcceptable(*I))
Chris Lattner209aeca2010-01-13 06:38:18 +000066 MangleLetter(OutName, *I);
Chris Lattner4b155fa2005-11-10 19:30:07 +000067 else
Chris Lattner209aeca2010-01-13 06:38:18 +000068 OutName.push_back(*I);
Chris Lattnere1d34bac2005-11-10 21:40:01 +000069 }
Chris Lattner209aeca2010-01-13 06:38:18 +000070 return;
Chris Lattner4b155fa2005-11-10 19:30:07 +000071 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000072
73 bool NeedPrefix = true;
74 bool NeedQuotes = false;
Chris Lattner04a7ce82010-01-13 04:55:33 +000075 StringRef::iterator I = X.begin();
Evan Chengcfdbfcc2009-05-05 22:50:29 +000076 if (*I == 1) {
77 NeedPrefix = false;
78 ++I; // Skip over the marker.
79 }
80
81 // If the first character is a number, we need quotes.
82 if (*I >= '0' && *I <= '9')
83 NeedQuotes = true;
84
85 // Do an initial scan of the string, checking to see if we need quotes or
86 // to escape a '"' or not.
87 if (!NeedQuotes)
Chris Lattner04a7ce82010-01-13 04:55:33 +000088 for (StringRef::iterator E = X.end(); I != E; ++I)
Evan Chengcfdbfcc2009-05-05 22:50:29 +000089 if (!isCharAcceptable(*I)) {
90 NeedQuotes = true;
91 break;
92 }
93
94 // In the common case, we don't need quotes. Handle this quickly.
95 if (!NeedQuotes) {
Chris Lattner209aeca2010-01-13 06:38:18 +000096 if (!NeedPrefix) {
97 OutName.append(X.begin()+1, X.end()); // Strip off the \001.
98 return;
99 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000100
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000101 if (PrefixTy == Mangler::Private)
Chris Lattner209aeca2010-01-13 06:38:18 +0000102 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000103 else if (PrefixTy == Mangler::LinkerPrivate)
Chris Lattner209aeca2010-01-13 06:38:18 +0000104 OutName.append(LinkerPrivatePrefix,
105 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
106
107 if (Prefix[0] == 0)
108 ; // Common noop, no prefix.
109 else if (Prefix[1] == 0)
110 OutName.push_back(Prefix[0]); // Common, one character prefix.
111 else
112 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix.
113 OutName.append(X.begin(), X.end());
114 return;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000115 }
Devang Patelc6faffd2009-08-17 23:17:17 +0000116
Chris Lattner209aeca2010-01-13 06:38:18 +0000117 // Add leading quote.
118 OutName.push_back('"');
119
120 // Add prefixes unless disabled.
121 if (NeedPrefix) {
122 if (PrefixTy == Mangler::Private)
123 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
124 else if (PrefixTy == Mangler::LinkerPrivate)
125 OutName.append(LinkerPrivatePrefix,
126 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));
127 OutName.append(Prefix, Prefix+strlen(Prefix));
128 }
129
130 // Add the piece that we already scanned through.
131 OutName.append(X.begin(), I);
132
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000133 // Otherwise, construct the string the expensive way.
Chris Lattner04a7ce82010-01-13 04:55:33 +0000134 for (StringRef::iterator E = X.end(); I != E; ++I) {
Chris Lattner209aeca2010-01-13 06:38:18 +0000135 if (*I == '"') {
136 const char *Quote = "_QQ_";
137 OutName.append(Quote, Quote+4);
138 } else if (*I == '\n') {
139 const char *Newline = "_NL_";
140 OutName.append(Newline, Newline+4);
141 } else
142 OutName.push_back(*I);
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000143 }
144
Chris Lattner209aeca2010-01-13 06:38:18 +0000145 // Add trailing quote.
146 OutName.push_back('"');
Brian Gaeked4dff192003-07-24 20:20:58 +0000147}
148
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000149/// getMangledName - Returns the mangled name of V, an LLVM Value,
150/// in the current module. If 'Suffix' is specified, the name ends with the
151/// specified suffix. If 'ForcePrivate' is specified, the label is specified
152/// to have a private label prefix.
153///
154std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
155 bool ForcePrivate) {
Chris Lattner05f19762009-07-14 00:15:14 +0000156 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
157 "Intrinsic functions cannot be mangled by Mangler");
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000158
159 ManglerPrefixTy PrefixTy =
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000160 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
161 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000162
Chris Lattner209aeca2010-01-13 06:38:18 +0000163 SmallString<128> Result;
164 if (GV->hasName()) {
165 makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy);
166 return Result.str().str();
167 }
Chris Lattner105efaf2009-07-14 00:01:06 +0000168
169 // Get the ID for the global, assigning a new one if we haven't got one
170 // already.
171 unsigned &ID = AnonGlobalIDs[GV];
172 if (ID == 0) ID = NextAnonGlobalID++;
173
174 // Must mangle the global into a unique ID.
Chris Lattner209aeca2010-01-13 06:38:18 +0000175 makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy);
176 return Result.str().str();
Brian Gaeked4dff192003-07-24 20:20:58 +0000177}
178
Chris Lattner840c8d72009-09-11 05:40:42 +0000179
180/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
181/// and the specified global variable's name. If the global variable doesn't
182/// have a name, this fills in a unique name for the global.
183void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
184 const GlobalValue *GV,
185 bool isImplicitlyPrivate) {
186
187 // If the global is anonymous or not led with \1, then add the appropriate
188 // prefix.
189 if (!GV->hasName() || GV->getName()[0] != '\1') {
Chris Lattner840c8d72009-09-11 05:40:42 +0000190 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
191 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
192 else if (GV->hasLinkerPrivateLinkage())
193 OutName.append(LinkerPrivatePrefix,
194 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
Chris Lattnerc827f532009-09-11 05:51:29 +0000195 OutName.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner840c8d72009-09-11 05:40:42 +0000196 }
197
198 // If the global has a name, just append it now.
199 if (GV->hasName()) {
200 StringRef Name = GV->getName();
201
202 // Strip off the prefix marker if present.
203 if (Name[0] != '\1')
204 OutName.append(Name.begin(), Name.end());
205 else
206 OutName.append(Name.begin()+1, Name.end());
207 return;
208 }
209
210 // If the global variable doesn't have a name, return a unique name for the
211 // global based on a numbering.
212
213 // Get the ID for the global, assigning a new one if we haven't got one
214 // already.
215 unsigned &ID = AnonGlobalIDs[GV];
216 if (ID == 0) ID = NextAnonGlobalID++;
217
218 // Must mangle the global into a unique ID.
219 raw_svector_ostream(OutName) << "__unnamed_" << ID;
220}
221
222
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000223Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix,
224 const char *linkerPrivatePrefix)
225 : Prefix(prefix), PrivatePrefix(privatePrefix),
226 LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false),
Nuno Lopes2c7e72c2009-09-21 14:11:56 +0000227 SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) {
Owen Andersone2f23a32007-09-07 04:06:50 +0000228 std::fill(AcceptableChars, array_endof(AcceptableChars), 0);
Chris Lattnere1d34bac2005-11-10 21:40:01 +0000229
230 // Letters and numbers are acceptable.
231 for (unsigned char X = 'a'; X <= 'z'; ++X)
232 markCharAcceptable(X);
233 for (unsigned char X = 'A'; X <= 'Z'; ++X)
234 markCharAcceptable(X);
235 for (unsigned char X = '0'; X <= '9'; ++X)
236 markCharAcceptable(X);
237
238 // These chars are acceptable.
239 markCharAcceptable('_');
240 markCharAcceptable('$');
241 markCharAcceptable('.');
Chris Lattner3d6c8eb2009-09-13 18:04:46 +0000242 markCharAcceptable('@');
Brian Gaeked4dff192003-07-24 20:20:58 +0000243}