Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 1 | //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 9 | // |
Rafael Espindola | 9794fdf | 2010-01-16 20:27:59 +0000 | [diff] [blame] | 10 | // Unified name mangler for CWriter and assembly backends. |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Brian Gaeke | b0078fa | 2003-07-25 20:21:20 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Mangler.h" |
Rafael Espindola | 9794fdf | 2010-01-16 20:27:59 +0000 | [diff] [blame] | 15 | #include "llvm/Function.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | b252cbb | 2010-01-13 05:02:57 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c9499b6 | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Rafael Espindola | 9794fdf | 2010-01-16 20:27:59 +0000 | [diff] [blame] | 23 | static char HexDigit(int V) { |
| 24 | return V < 10 ? V+'0' : V+'A'-10; |
| 25 | } |
| 26 | |
| 27 | static 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('_'); |
| 32 | } |
| 33 | |
| 34 | /// makeNameProper - We don't want identifier names non-C-identifier characters |
| 35 | /// in them, so mangle them as appropriate. |
| 36 | /// |
| 37 | /// FIXME: This is deprecated, new code should use getNameWithPrefix and use |
| 38 | /// MCSymbol printing to handle quotes or not etc. |
| 39 | /// |
| 40 | void Mangler::makeNameProper(SmallVectorImpl<char> &OutName, |
| 41 | const Twine &TheName, |
| 42 | ManglerPrefixTy PrefixTy) { |
| 43 | SmallString<256> TmpData; |
| 44 | StringRef X = TheName.toStringRef(TmpData); |
| 45 | assert(!X.empty() && "Cannot mangle empty strings"); |
| 46 | |
| 47 | if (!UseQuotes) { |
| 48 | // If X does not start with (char)1, add the prefix. |
| 49 | StringRef::iterator I = X.begin(); |
| 50 | if (*I == 1) { |
| 51 | ++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)); |
| 59 | } |
| 60 | |
| 61 | // Mangle the first letter specially, don't allow numbers unless the target |
| 62 | // explicitly allows them. |
| 63 | if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9') |
| 64 | MangleLetter(OutName, *I++); |
| 65 | |
| 66 | for (StringRef::iterator E = X.end(); I != E; ++I) { |
| 67 | if (!isCharAcceptable(*I)) |
| 68 | MangleLetter(OutName, *I); |
| 69 | else |
| 70 | OutName.push_back(*I); |
| 71 | } |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | bool NeedPrefix = true; |
| 76 | bool NeedQuotes = false; |
| 77 | StringRef::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 (StringRef::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) { |
| 98 | if (!NeedPrefix) { |
| 99 | OutName.append(X.begin()+1, X.end()); // Strip off the \001. |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (PrefixTy == Mangler::Private) |
| 104 | OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix)); |
| 105 | else if (PrefixTy == Mangler::LinkerPrivate) |
| 106 | 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; |
| 117 | } |
| 118 | |
| 119 | // 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. |
| 133 | OutName.append(X.begin()+!NeedPrefix, I); |
| 134 | |
| 135 | // Otherwise, construct the string the expensive way. |
| 136 | for (StringRef::iterator E = X.end(); I != E; ++I) { |
| 137 | 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); |
| 145 | } |
| 146 | |
| 147 | // Add trailing quote. |
| 148 | OutName.push_back('"'); |
| 149 | } |
| 150 | |
| 151 | /// 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 | /// |
| 156 | /// FIXME: This is deprecated, new code should use getNameWithPrefix and use |
| 157 | /// MCSymbol printing to handle quotes or not etc. |
| 158 | /// |
| 159 | std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix, |
| 160 | bool ForcePrivate) { |
| 161 | assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) && |
| 162 | "Intrinsic functions cannot be mangled by Mangler"); |
| 163 | |
| 164 | ManglerPrefixTy PrefixTy = |
| 165 | (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private : |
| 166 | GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default; |
| 167 | |
| 168 | SmallString<128> Result; |
| 169 | if (GV->hasName()) { |
| 170 | makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy); |
| 171 | return Result.str().str(); |
| 172 | } |
| 173 | |
| 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. |
| 180 | makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy); |
| 181 | return Result.str().str(); |
| 182 | } |
| 183 | |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 184 | /// 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. |
| 187 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 188 | const Twine &GVName, ManglerPrefixTy PrefixTy) { |
| 189 | SmallString<256> TmpData; |
Benjamin Kramer | 2e06b93 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 190 | StringRef Name = GVName.toStringRef(TmpData); |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 191 | 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 Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 214 | |
| 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. |
| 218 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 219 | const GlobalValue *GV, |
| 220 | bool isImplicitlyPrivate) { |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 221 | // If this global has a name, handle it simply. |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 222 | if (GV->hasName()) { |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 223 | ManglerPrefixTy PrefixTy = Mangler::Default; |
| 224 | if (GV->hasPrivateLinkage() || isImplicitlyPrivate) |
| 225 | PrefixTy = Mangler::Private; |
| 226 | else if (GV->hasLinkerPrivateLinkage()) |
| 227 | PrefixTy = Mangler::LinkerPrivate; |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 229 | return getNameWithPrefix(OutName, GV->getName(), PrefixTy); |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 230 | } |
| 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 Lattner | 33535b3 | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 235 | // 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 Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 243 | // 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 | |
Chris Lattner | d3a2163 | 2010-01-16 18:06:34 +0000 | [diff] [blame] | 252 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 253 | /// and the specified global variable's name. If the global variable doesn't |
| 254 | /// have a name, this fills in a unique name for the global. |
| 255 | std::string Mangler::getNameWithPrefix(const GlobalValue *GV, |
| 256 | bool isImplicitlyPrivate) { |
| 257 | SmallString<64> Buf; |
| 258 | getNameWithPrefix(Buf, GV, isImplicitlyPrivate); |
| 259 | return std::string(Buf.begin(), Buf.end()); |
| 260 | } |
| 261 | |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 262 | |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 263 | Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix, |
| 264 | const char *linkerPrivatePrefix) |
| 265 | : Prefix(prefix), PrivatePrefix(privatePrefix), |
Rafael Espindola | 9794fdf | 2010-01-16 20:27:59 +0000 | [diff] [blame] | 266 | LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false), |
| 267 | SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) { |
| 268 | std::fill(AcceptableChars, array_endof(AcceptableChars), 0); |
| 269 | |
| 270 | // Letters and numbers are acceptable. |
| 271 | for (unsigned char X = 'a'; X <= 'z'; ++X) |
| 272 | markCharAcceptable(X); |
| 273 | for (unsigned char X = 'A'; X <= 'Z'; ++X) |
| 274 | markCharAcceptable(X); |
| 275 | for (unsigned char X = '0'; X <= '9'; ++X) |
| 276 | markCharAcceptable(X); |
| 277 | |
| 278 | // These chars are acceptable. |
| 279 | markCharAcceptable('_'); |
| 280 | markCharAcceptable('$'); |
| 281 | markCharAcceptable('.'); |
| 282 | markCharAcceptable('@'); |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 283 | } |