Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 1 | //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | c94c825 | 2010-01-16 21:08:46 +0000 | [diff] [blame] | 10 | // Unified name mangler for assembly backends. |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 45111d1 | 2010-01-16 21:57:06 +0000 | [diff] [blame^] | 14 | #include "llvm/Target/Mangler.h" |
Chris Lattner | c94c825 | 2010-01-16 21:08:46 +0000 | [diff] [blame] | 15 | #include "llvm/GlobalValue.h" |
Chris Lattner | 36e69ae | 2010-01-13 05:02:57 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | c94c825 | 2010-01-16 21:08:46 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 21 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 22 | /// and the specified name as the global variable name. GVName must not be |
| 23 | /// empty. |
| 24 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 25 | const Twine &GVName, ManglerPrefixTy PrefixTy) { |
| 26 | SmallString<256> TmpData; |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 27 | StringRef Name = GVName.toStringRef(TmpData); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 28 | assert(!Name.empty() && "getNameWithPrefix requires non-empty name"); |
| 29 | |
| 30 | // If the global name is not led with \1, add the appropriate prefixes. |
| 31 | if (Name[0] != '\1') { |
| 32 | if (PrefixTy == Mangler::Private) |
| 33 | OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix)); |
| 34 | else if (PrefixTy == Mangler::LinkerPrivate) |
| 35 | OutName.append(LinkerPrivatePrefix, |
| 36 | LinkerPrivatePrefix+strlen(LinkerPrivatePrefix)); |
| 37 | |
| 38 | if (Prefix[0] == 0) |
| 39 | ; // Common noop, no prefix. |
| 40 | else if (Prefix[1] == 0) |
| 41 | OutName.push_back(Prefix[0]); // Common, one character prefix. |
| 42 | else |
| 43 | OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary prefix. |
| 44 | } else { |
| 45 | Name = Name.substr(1); |
| 46 | } |
| 47 | |
| 48 | OutName.append(Name.begin(), Name.end()); |
| 49 | } |
| 50 | |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 51 | |
| 52 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 53 | /// and the specified global variable's name. If the global variable doesn't |
| 54 | /// have a name, this fills in a unique name for the global. |
| 55 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 56 | const GlobalValue *GV, |
| 57 | bool isImplicitlyPrivate) { |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 58 | // If this global has a name, handle it simply. |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 59 | if (GV->hasName()) { |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 60 | ManglerPrefixTy PrefixTy = Mangler::Default; |
| 61 | if (GV->hasPrivateLinkage() || isImplicitlyPrivate) |
| 62 | PrefixTy = Mangler::Private; |
| 63 | else if (GV->hasLinkerPrivateLinkage()) |
| 64 | PrefixTy = Mangler::LinkerPrivate; |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 66 | return getNameWithPrefix(OutName, GV->getName(), PrefixTy); |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // If the global variable doesn't have a name, return a unique name for the |
| 70 | // global based on a numbering. |
| 71 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 72 | // Anonymous names always get prefixes. |
| 73 | if (GV->hasPrivateLinkage() || isImplicitlyPrivate) |
| 74 | OutName.append(PrivatePrefix, PrivatePrefix+strlen(PrivatePrefix)); |
| 75 | else if (GV->hasLinkerPrivateLinkage()) |
| 76 | OutName.append(LinkerPrivatePrefix, |
| 77 | LinkerPrivatePrefix+strlen(LinkerPrivatePrefix));; |
| 78 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
| 79 | |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 80 | // Get the ID for the global, assigning a new one if we haven't got one |
| 81 | // already. |
| 82 | unsigned &ID = AnonGlobalIDs[GV]; |
| 83 | if (ID == 0) ID = NextAnonGlobalID++; |
| 84 | |
| 85 | // Must mangle the global into a unique ID. |
| 86 | raw_svector_ostream(OutName) << "__unnamed_" << ID; |
| 87 | } |
| 88 | |
Chris Lattner | 61f160a | 2010-01-16 18:06:34 +0000 | [diff] [blame] | 89 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 90 | /// and the specified global variable's name. If the global variable doesn't |
| 91 | /// have a name, this fills in a unique name for the global. |
| 92 | std::string Mangler::getNameWithPrefix(const GlobalValue *GV, |
| 93 | bool isImplicitlyPrivate) { |
| 94 | SmallString<64> Buf; |
| 95 | getNameWithPrefix(Buf, GV, isImplicitlyPrivate); |
| 96 | return std::string(Buf.begin(), Buf.end()); |
| 97 | } |
| 98 | |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 99 | |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 100 | Mangler::Mangler(Module &M, const char *prefix, const char *privatePrefix, |
| 101 | const char *linkerPrivatePrefix) |
| 102 | : Prefix(prefix), PrivatePrefix(privatePrefix), |
Chris Lattner | c94c825 | 2010-01-16 21:08:46 +0000 | [diff] [blame] | 103 | LinkerPrivatePrefix(linkerPrivatePrefix), NextAnonGlobalID(1) { |
Brian Gaeke | b198ca3 | 2003-07-24 20:20:58 +0000 | [diff] [blame] | 104 | } |