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 | c0dba72 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 36e69ae | 2010-01-13 05:02:57 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | c94c825 | 2010-01-16 21:08:46 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.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 | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame^] | 21 | static bool isAcceptableChar(char C) { |
| 22 | if ((C < 'a' || C > 'z') && |
| 23 | (C < 'A' || C > 'Z') && |
| 24 | (C < '0' || C > '9') && |
| 25 | C != '_' && C != '$' && C != '.' && C != '@') |
| 26 | return false; |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | static char HexDigit(int V) { |
| 31 | return V < 10 ? V+'0' : V+'A'-10; |
| 32 | } |
| 33 | |
| 34 | static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) { |
| 35 | OutName.push_back('_'); |
| 36 | OutName.push_back(HexDigit(C >> 4)); |
| 37 | OutName.push_back(HexDigit(C & 15)); |
| 38 | OutName.push_back('_'); |
| 39 | } |
| 40 | |
| 41 | /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes |
| 42 | /// for this assembler. |
| 43 | static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) { |
| 44 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
| 45 | |
| 46 | // If the first character is a number and the target does not allow this, we |
| 47 | // need quotes. |
| 48 | if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') |
| 49 | return true; |
| 50 | |
| 51 | // If any of the characters in the string is an unacceptable character, force |
| 52 | // quotes. |
| 53 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 54 | if (!isAcceptableChar(Str[i])) |
| 55 | return true; |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | /// appendMangledName - Add the specified string in mangled form if it uses |
| 60 | /// any unusual characters. |
| 61 | void Mangler::appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str, |
| 62 | const MCAsmInfo *MAI) { |
| 63 | // The first character is not allowed to be a number unless the target |
| 64 | // explicitly allows it. |
| 65 | if ((MAI == 0 || !MAI->doesAllowNameToStartWithDigit()) && |
| 66 | Str[0] >= '0' && Str[0] <= '9') { |
| 67 | MangleLetter(OutName, Str[0]); |
| 68 | Str = Str.substr(1); |
| 69 | } |
| 70 | |
| 71 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 72 | if (!isAcceptableChar(Str[i])) |
| 73 | MangleLetter(OutName, Str[i]); |
| 74 | else |
| 75 | OutName.push_back(Str[i]); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /// appendMangledQuotedName - On systems that support quoted symbols, we still |
| 81 | /// have to escape some (obscure) characters like " and \n which would break the |
| 82 | /// assembler's lexing. |
| 83 | static void appendMangledQuotedName(SmallVectorImpl<char> &OutName, |
| 84 | StringRef Str) { |
| 85 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 86 | if (Str[i] == '"' || Str[i] == '\n') |
| 87 | MangleLetter(OutName, Str[i]); |
| 88 | else |
| 89 | OutName.push_back(Str[i]); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 94 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 95 | /// and the specified name as the global variable name. GVName must not be |
| 96 | /// empty. |
| 97 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 98 | const Twine &GVName, ManglerPrefixTy PrefixTy) { |
| 99 | SmallString<256> TmpData; |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 100 | StringRef Name = GVName.toStringRef(TmpData); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 101 | assert(!Name.empty() && "getNameWithPrefix requires non-empty name"); |
| 102 | |
| 103 | // If the global name is not led with \1, add the appropriate prefixes. |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame^] | 104 | if (Name[0] == '\1') { |
| 105 | Name = Name.substr(1); |
| 106 | } else { |
Chris Lattner | c0dba72 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 107 | if (PrefixTy == Mangler::Private) { |
| 108 | const char *Prefix = MAI.getPrivateGlobalPrefix(); |
| 109 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
| 110 | } else if (PrefixTy == Mangler::LinkerPrivate) { |
| 111 | const char *Prefix = MAI.getLinkerPrivateGlobalPrefix(); |
| 112 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
| 113 | } |
| 114 | |
| 115 | const char *Prefix = MAI.getGlobalPrefix(); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 116 | if (Prefix[0] == 0) |
| 117 | ; // Common noop, no prefix. |
| 118 | else if (Prefix[1] == 0) |
| 119 | OutName.push_back(Prefix[0]); // Common, one character prefix. |
| 120 | else |
Chris Lattner | c0dba72 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 121 | OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix. |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame^] | 124 | // If this is a simple string that doesn't need escaping, just append it. |
| 125 | if (!NameNeedsEscaping(Name, MAI) || |
| 126 | // If quotes are supported, they can be used unless the string contains |
| 127 | // a quote or newline. |
| 128 | (MAI.doesAllowQuotesInName() && |
| 129 | Name.find_first_of("\n\"") == StringRef::npos)) { |
| 130 | OutName.append(Name.begin(), Name.end()); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // On systems that do not allow quoted names, we need to mangle most |
| 135 | // strange characters. |
| 136 | if (!MAI.doesAllowQuotesInName()) |
| 137 | return appendMangledName(OutName, Name, &MAI); |
| 138 | |
| 139 | // Okay, the system allows quoted strings. We can quote most anything, the |
| 140 | // only characters that need escaping are " and \n. |
| 141 | assert(Name.find_first_of("\n\"") != StringRef::npos); |
| 142 | return appendMangledQuotedName(OutName, Name); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 145 | |
| 146 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 147 | /// and the specified global variable's name. If the global variable doesn't |
| 148 | /// have a name, this fills in a unique name for the global. |
| 149 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 150 | const GlobalValue *GV, |
| 151 | bool isImplicitlyPrivate) { |
Chris Lattner | ff24005 | 2010-01-17 18:52:16 +0000 | [diff] [blame] | 152 | ManglerPrefixTy PrefixTy = Mangler::Default; |
| 153 | if (GV->hasPrivateLinkage() || isImplicitlyPrivate) |
| 154 | PrefixTy = Mangler::Private; |
| 155 | else if (GV->hasLinkerPrivateLinkage()) |
| 156 | PrefixTy = Mangler::LinkerPrivate; |
| 157 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 158 | // If this global has a name, handle it simply. |
Chris Lattner | ff24005 | 2010-01-17 18:52:16 +0000 | [diff] [blame] | 159 | if (GV->hasName()) |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 160 | return getNameWithPrefix(OutName, GV->getName(), PrefixTy); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 162 | // Get the ID for the global, assigning a new one if we haven't got one |
| 163 | // already. |
| 164 | unsigned &ID = AnonGlobalIDs[GV]; |
| 165 | if (ID == 0) ID = NextAnonGlobalID++; |
| 166 | |
| 167 | // Must mangle the global into a unique ID. |
Chris Lattner | ff24005 | 2010-01-17 18:52:16 +0000 | [diff] [blame] | 168 | getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy); |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | 61f160a | 2010-01-16 18:06:34 +0000 | [diff] [blame] | 171 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 172 | /// and the specified global variable's name. If the global variable doesn't |
| 173 | /// have a name, this fills in a unique name for the global. |
| 174 | std::string Mangler::getNameWithPrefix(const GlobalValue *GV, |
| 175 | bool isImplicitlyPrivate) { |
| 176 | SmallString<64> Buf; |
| 177 | getNameWithPrefix(Buf, GV, isImplicitlyPrivate); |
| 178 | return std::string(Buf.begin(), Buf.end()); |
| 179 | } |