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 | // |
| 10 | // Unified name mangler for CWriter and assembly backends. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Brian Gaeke | b0078fa | 2003-07-25 20:21:20 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Mangler.h" |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 15 | #include "llvm/Function.h" |
Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Evan Cheng | 0e69734 | 2008-07-10 00:04:23 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 9527fea | 2003-08-24 21:08:38 +0000 | [diff] [blame] | 23 | static char HexDigit(int V) { |
| 24 | return V < 10 ? V+'0' : V+'A'-10; |
| 25 | } |
| 26 | |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 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('_'); |
Chris Lattner | 9527fea | 2003-08-24 21:08:38 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// makeNameProper - We don't want identifier names non-C-identifier characters |
| 35 | /// in them, so mangle them as appropriate. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 36 | /// |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 37 | void Mangler::makeNameProper(SmallVectorImpl<char> &OutName, |
| 38 | const Twine &TheName, |
| 39 | ManglerPrefixTy PrefixTy) { |
Chris Lattner | b252cbb | 2010-01-13 05:02:57 +0000 | [diff] [blame] | 40 | SmallString<256> TmpData; |
| 41 | TheName.toVector(TmpData); |
| 42 | StringRef X = TmpData.str(); |
Chris Lattner | f34815b | 2009-07-14 04:50:12 +0000 | [diff] [blame] | 43 | assert(!X.empty() && "Cannot mangle empty strings"); |
Chris Lattner | cc9c033 | 2005-09-24 08:24:28 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 4b155fa | 2005-11-10 19:30:07 +0000 | [diff] [blame] | 45 | if (!UseQuotes) { |
| 46 | // If X does not start with (char)1, add the prefix. |
Chris Lattner | 04a7ce8 | 2010-01-13 04:55:33 +0000 | [diff] [blame] | 47 | StringRef::iterator I = X.begin(); |
Evan Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 48 | if (*I == 1) { |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 49 | ++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 Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | 4b155fa | 2005-11-10 19:30:07 +0000 | [diff] [blame] | 58 | |
Anton Korobeynikov | 592638a | 2009-09-18 16:57:42 +0000 | [diff] [blame] | 59 | // Mangle the first letter specially, don't allow numbers unless the target |
| 60 | // explicitly allows them. |
| 61 | if (!SymbolsCanStartWithDigit && *I >= '0' && *I <= '9') |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 62 | MangleLetter(OutName, *I++); |
Chris Lattner | 4b155fa | 2005-11-10 19:30:07 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 04a7ce8 | 2010-01-13 04:55:33 +0000 | [diff] [blame] | 64 | for (StringRef::iterator E = X.end(); I != E; ++I) { |
Chris Lattner | e1d34bac | 2005-11-10 21:40:01 +0000 | [diff] [blame] | 65 | if (!isCharAcceptable(*I)) |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 66 | MangleLetter(OutName, *I); |
Chris Lattner | 4b155fa | 2005-11-10 19:30:07 +0000 | [diff] [blame] | 67 | else |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 68 | OutName.push_back(*I); |
Chris Lattner | e1d34bac | 2005-11-10 21:40:01 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 70 | return; |
Chris Lattner | 4b155fa | 2005-11-10 19:30:07 +0000 | [diff] [blame] | 71 | } |
Evan Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 72 | |
| 73 | bool NeedPrefix = true; |
| 74 | bool NeedQuotes = false; |
Chris Lattner | 04a7ce8 | 2010-01-13 04:55:33 +0000 | [diff] [blame] | 75 | StringRef::iterator I = X.begin(); |
Evan Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 76 | 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 Lattner | 04a7ce8 | 2010-01-13 04:55:33 +0000 | [diff] [blame] | 88 | for (StringRef::iterator E = X.end(); I != E; ++I) |
Evan Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 89 | 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 Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 96 | if (!NeedPrefix) { |
| 97 | OutName.append(X.begin()+1, X.end()); // Strip off the \001. |
| 98 | return; |
| 99 | } |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 100 | |
Bill Wendling | 1bcfbff | 2009-07-20 19:41:27 +0000 | [diff] [blame] | 101 | if (PrefixTy == Mangler::Private) |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 102 | OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix)); |
Bill Wendling | 1bcfbff | 2009-07-20 19:41:27 +0000 | [diff] [blame] | 103 | else if (PrefixTy == Mangler::LinkerPrivate) |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 104 | 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 Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 115 | } |
Devang Patel | c6faffd | 2009-08-17 23:17:17 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 117 | // 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 Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 133 | // Otherwise, construct the string the expensive way. |
Chris Lattner | 04a7ce8 | 2010-01-13 04:55:33 +0000 | [diff] [blame] | 134 | for (StringRef::iterator E = X.end(); I != E; ++I) { |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 135 | 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 Cheng | cfdbfcc | 2009-05-05 22:50:29 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 145 | // Add trailing quote. |
| 146 | OutName.push_back('"'); |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 8c9a96b | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 149 | /// 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 | /// |
| 154 | std::string Mangler::getMangledName(const GlobalValue *GV, const char *Suffix, |
| 155 | bool ForcePrivate) { |
Chris Lattner | 05f1976 | 2009-07-14 00:15:14 +0000 | [diff] [blame] | 156 | assert((!isa<Function>(GV) || !cast<Function>(GV)->isIntrinsic()) && |
| 157 | "Intrinsic functions cannot be mangled by Mangler"); |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 158 | |
| 159 | ManglerPrefixTy PrefixTy = |
Bill Wendling | 1bcfbff | 2009-07-20 19:41:27 +0000 | [diff] [blame] | 160 | (GV->hasPrivateLinkage() || ForcePrivate) ? Mangler::Private : |
| 161 | GV->hasLinkerPrivateLinkage() ? Mangler::LinkerPrivate : Mangler::Default; |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 163 | SmallString<128> Result; |
| 164 | if (GV->hasName()) { |
| 165 | makeNameProper(Result, GV->getNameStr() + Suffix, PrefixTy); |
| 166 | return Result.str().str(); |
| 167 | } |
Chris Lattner | 105efaf | 2009-07-14 00:01:06 +0000 | [diff] [blame] | 168 | |
| 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 Lattner | 209aeca | 2010-01-13 06:38:18 +0000 | [diff] [blame^] | 175 | makeNameProper(Result, "__unnamed_" + utostr(ID) + Suffix, PrefixTy); |
| 176 | return Result.str().str(); |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 179 | |
| 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. |
| 183 | void 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 Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 190 | 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 Lattner | c827f53 | 2009-09-11 05:51:29 +0000 | [diff] [blame] | 195 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
Chris Lattner | 840c8d7 | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 196 | } |
| 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 Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 223 | Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix, |
| 224 | const char *linkerPrivatePrefix) |
| 225 | : Prefix(prefix), PrivatePrefix(privatePrefix), |
| 226 | LinkerPrivatePrefix(linkerPrivatePrefix), UseQuotes(false), |
Nuno Lopes | 2c7e72c | 2009-09-21 14:11:56 +0000 | [diff] [blame] | 227 | SymbolsCanStartWithDigit(false), NextAnonGlobalID(1) { |
Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 228 | std::fill(AcceptableChars, array_endof(AcceptableChars), 0); |
Chris Lattner | e1d34bac | 2005-11-10 21:40:01 +0000 | [diff] [blame] | 229 | |
| 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 Lattner | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 242 | markCharAcceptable('@'); |
Brian Gaeke | d4dff19 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 243 | } |