blob: 21ba086d19f740e1cb58d56b6664b3e0ec7dbbb9 [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
Chris Lattnere1d34bac2005-11-10 21:40:01 +000050 // Mangle the first letter specially, don't allow numbers.
Chris Lattner4b155fa2005-11-10 19:30:07 +000051 if (*I >= '0' && *I <= '9')
52 Result += MangleLetter(*I++);
53
Chris Lattnere1d34bac2005-11-10 21:40:01 +000054 for (std::string::const_iterator E = X.end(); I != E; ++I) {
55 if (!isCharAcceptable(*I))
Chris Lattner4b155fa2005-11-10 19:30:07 +000056 Result += MangleLetter(*I);
57 else
58 Result += *I;
Chris Lattnere1d34bac2005-11-10 21:40:01 +000059 }
Chris Lattnerecc301d2005-11-10 23:24:26 +000060
Chris Lattner8c9a96b2009-07-14 18:17:16 +000061 if (NeedPrefix) {
62 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000063
Bill Wendling1bcfbff2009-07-20 19:41:27 +000064 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +000065 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +000066 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000067 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +000068 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +000069
Evan Chengcfdbfcc2009-05-05 22:50:29 +000070 return Result;
Chris Lattner4b155fa2005-11-10 19:30:07 +000071 }
Evan Chengcfdbfcc2009-05-05 22:50:29 +000072
73 bool NeedPrefix = true;
74 bool NeedQuotes = false;
75 std::string Result;
76 std::string::const_iterator I = X.begin();
77 if (*I == 1) {
78 NeedPrefix = false;
79 ++I; // Skip over the marker.
80 }
81
82 // If the first character is a number, we need quotes.
83 if (*I >= '0' && *I <= '9')
84 NeedQuotes = true;
85
86 // Do an initial scan of the string, checking to see if we need quotes or
87 // to escape a '"' or not.
88 if (!NeedQuotes)
89 for (std::string::const_iterator E = X.end(); I != E; ++I)
90 if (!isCharAcceptable(*I)) {
91 NeedQuotes = true;
92 break;
93 }
94
95 // In the common case, we don't need quotes. Handle this quickly.
96 if (!NeedQuotes) {
Chris Lattnerf34815b2009-07-14 04:50:12 +000097 if (!NeedPrefix)
98 return X.substr(1); // Strip off the \001.
Chris Lattner8c9a96b2009-07-14 18:17:16 +000099
100 Result = Prefix + X;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000101
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000102 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000103 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000104 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000105 Result = LinkerPrivatePrefix + Result;
106
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000107 return Result;
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000108 }
Devang Patelc6faffd2009-08-17 23:17:17 +0000109
110 if (NeedPrefix)
111 Result = X.substr(0, I-X.begin());
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000112
113 // Otherwise, construct the string the expensive way.
114 for (std::string::const_iterator E = X.end(); I != E; ++I) {
115 if (*I == '"')
116 Result += "_QQ_";
117 else if (*I == '\n')
118 Result += "_NL_";
119 else
120 Result += *I;
121 }
122
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000123 if (NeedPrefix) {
124 Result = Prefix + Result;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000125
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000126 if (PrefixTy == Mangler::Private)
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000127 Result = PrivatePrefix + Result;
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000128 else if (PrefixTy == Mangler::LinkerPrivate)
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000129 Result = LinkerPrivatePrefix + Result;
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000130 }
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000131
Evan Chengcfdbfcc2009-05-05 22:50:29 +0000132 Result = '"' + Result + '"';
Chris Lattner9527fea2003-08-24 21:08:38 +0000133 return Result;
Brian Gaeked4dff192003-07-24 20:20:58 +0000134}
135
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000136/// getMangledName - Returns the mangled name of V, an LLVM Value,
137/// in the current module. If 'Suffix' is specified, the name ends with the
138/// specified suffix. If 'ForcePrivate' is specified, the label is specified
139/// to have a private label prefix.
140///
141std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix,
142 bool ForcePrivate) {
Chris Lattner05f19762009-07-14 00:15:14 +0000143 assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) &&
144 "Intrinsic functions cannot be mangled by Mangler");
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000145
146 ManglerPrefixTy PrefixTy =
Bill Wendling1bcfbff2009-07-20 19:41:27 +0000147 (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private :
148 GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000149
Chris Lattnerf34815b2009-07-14 04:50:12 +0000150 if (GV->hasName())
Daniel Dunbar5899dda2009-07-22 21:33:09 +0000151 return makeNameProper(GV->getNameStr() + Suffix, PrefixTy);
Chris Lattner105efaf2009-07-14 00:01:06 +0000152
153 // Get the ID for the global, assigning a new one if we haven't got one
154 // already.
155 unsigned &ID = AnonGlobalIDs[GV];
156 if (ID == 0) ID = NextAnonGlobalID++;
157
158 // Must mangle the global into a unique ID.
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000159 return makeNameProper("__unnamed_" + utostr(ID) + Suffix, PrefixTy);
Brian Gaeked4dff192003-07-24 20:20:58 +0000160}
161
Chris Lattner840c8d72009-09-11 05:40:42 +0000162
163/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
164/// and the specified global variable's name. If the global variable doesn't
165/// have a name, this fills in a unique name for the global.
166void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
167 const GlobalValue *GV,
168 bool isImplicitlyPrivate) {
169
170 // If the global is anonymous or not led with \1, then add the appropriate
171 // prefix.
172 if (!GV->hasName() || GV->getName()[0] != '\1') {
173 OutName.append(Prefix, Prefix+strlen(Prefix));
174
175 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
176 OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix));
177 else if (GV->hasLinkerPrivateLinkage())
178 OutName.append(LinkerPrivatePrefix,
179 LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));;
180 }
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('.');
Brian Gaeked4dff192003-07-24 20:20:58 +0000226}