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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/Twine.h" |
| 17 | #include "llvm/DataLayout.h" |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
Chris Lattner | c0dba72 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Sean Hunt | 3420e7f | 2012-04-07 00:37:53 +0000 | [diff] [blame] | 25 | static bool isAcceptableChar(char C, bool AllowPeriod, bool AllowUTF8) { |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 26 | if ((C < 'a' || C > 'z') && |
| 27 | (C < 'A' || C > 'Z') && |
| 28 | (C < '0' || C > '9') && |
Mon P Wang | b9a01bc | 2010-04-29 04:00:56 +0000 | [diff] [blame] | 29 | C != '_' && C != '$' && C != '@' && |
Sean Hunt | 3420e7f | 2012-04-07 00:37:53 +0000 | [diff] [blame] | 30 | !(AllowPeriod && C == '.') && |
| 31 | !(AllowUTF8 && (C & 0x80))) |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 32 | return false; |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | static char HexDigit(int V) { |
| 37 | return V < 10 ? V+'0' : V+'A'-10; |
| 38 | } |
| 39 | |
| 40 | static void MangleLetter(SmallVectorImpl<char> &OutName, unsigned char C) { |
| 41 | OutName.push_back('_'); |
| 42 | OutName.push_back(HexDigit(C >> 4)); |
| 43 | OutName.push_back(HexDigit(C & 15)); |
| 44 | OutName.push_back('_'); |
| 45 | } |
| 46 | |
Dmitri Gribenko | c5252da | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 47 | /// NameNeedsEscaping - Return true if the identifier \p Str needs quotes |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 48 | /// for this assembler. |
| 49 | static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) { |
| 50 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
| 51 | |
| 52 | // If the first character is a number and the target does not allow this, we |
| 53 | // need quotes. |
| 54 | if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') |
| 55 | return true; |
| 56 | |
| 57 | // If any of the characters in the string is an unacceptable character, force |
| 58 | // quotes. |
Mon P Wang | b9a01bc | 2010-04-29 04:00:56 +0000 | [diff] [blame] | 59 | bool AllowPeriod = MAI.doesAllowPeriodsInName(); |
Sean Hunt | 3420e7f | 2012-04-07 00:37:53 +0000 | [diff] [blame] | 60 | bool AllowUTF8 = MAI.doesAllowUTF8(); |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 61 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
Sean Hunt | 3420e7f | 2012-04-07 00:37:53 +0000 | [diff] [blame] | 62 | if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8)) |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 63 | return true; |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | /// appendMangledName - Add the specified string in mangled form if it uses |
| 68 | /// any unusual characters. |
Chris Lattner | 0bd58b0 | 2010-01-17 19:32:29 +0000 | [diff] [blame] | 69 | static void appendMangledName(SmallVectorImpl<char> &OutName, StringRef Str, |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 70 | const MCAsmInfo &MAI) { |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 71 | // The first character is not allowed to be a number unless the target |
| 72 | // explicitly allows it. |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 73 | if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') { |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 74 | MangleLetter(OutName, Str[0]); |
| 75 | Str = Str.substr(1); |
| 76 | } |
Mon P Wang | b9a01bc | 2010-04-29 04:00:56 +0000 | [diff] [blame] | 77 | |
| 78 | bool AllowPeriod = MAI.doesAllowPeriodsInName(); |
Sean Hunt | 3420e7f | 2012-04-07 00:37:53 +0000 | [diff] [blame] | 79 | bool AllowUTF8 = MAI.doesAllowUTF8(); |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 80 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
Sean Hunt | 3420e7f | 2012-04-07 00:37:53 +0000 | [diff] [blame] | 81 | if (!isAcceptableChar(Str[i], AllowPeriod, AllowUTF8)) |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 82 | MangleLetter(OutName, Str[i]); |
| 83 | else |
| 84 | OutName.push_back(Str[i]); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /// appendMangledQuotedName - On systems that support quoted symbols, we still |
| 90 | /// have to escape some (obscure) characters like " and \n which would break the |
| 91 | /// assembler's lexing. |
| 92 | static void appendMangledQuotedName(SmallVectorImpl<char> &OutName, |
| 93 | StringRef Str) { |
| 94 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 95 | if (Str[i] == '"' || Str[i] == '\n') |
| 96 | MangleLetter(OutName, Str[i]); |
| 97 | else |
| 98 | OutName.push_back(Str[i]); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 103 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 104 | /// and the specified name as the global variable name. GVName must not be |
| 105 | /// empty. |
| 106 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 107 | const Twine &GVName, ManglerPrefixTy PrefixTy) { |
| 108 | SmallString<256> TmpData; |
Benjamin Kramer | b357e06 | 2010-01-13 12:45:23 +0000 | [diff] [blame] | 109 | StringRef Name = GVName.toStringRef(TmpData); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 110 | assert(!Name.empty() && "getNameWithPrefix requires non-empty name"); |
| 111 | |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 112 | const MCAsmInfo &MAI = Context.getAsmInfo(); |
| 113 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 114 | // 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] | 115 | if (Name[0] == '\1') { |
| 116 | Name = Name.substr(1); |
| 117 | } else { |
Chris Lattner | c0dba72 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 118 | if (PrefixTy == Mangler::Private) { |
| 119 | const char *Prefix = MAI.getPrivateGlobalPrefix(); |
| 120 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
| 121 | } else if (PrefixTy == Mangler::LinkerPrivate) { |
| 122 | const char *Prefix = MAI.getLinkerPrivateGlobalPrefix(); |
| 123 | OutName.append(Prefix, Prefix+strlen(Prefix)); |
| 124 | } |
| 125 | |
| 126 | const char *Prefix = MAI.getGlobalPrefix(); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 127 | if (Prefix[0] == 0) |
| 128 | ; // Common noop, no prefix. |
| 129 | else if (Prefix[1] == 0) |
| 130 | OutName.push_back(Prefix[0]); // Common, one character prefix. |
| 131 | else |
Chris Lattner | c0dba72 | 2010-01-17 18:22:35 +0000 | [diff] [blame] | 132 | OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix. |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 135 | // If this is a simple string that doesn't need escaping, just append it. |
| 136 | if (!NameNeedsEscaping(Name, MAI) || |
| 137 | // If quotes are supported, they can be used unless the string contains |
| 138 | // a quote or newline. |
| 139 | (MAI.doesAllowQuotesInName() && |
| 140 | Name.find_first_of("\n\"") == StringRef::npos)) { |
| 141 | OutName.append(Name.begin(), Name.end()); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // On systems that do not allow quoted names, we need to mangle most |
| 146 | // strange characters. |
| 147 | if (!MAI.doesAllowQuotesInName()) |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 148 | return appendMangledName(OutName, Name, MAI); |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 149 | |
| 150 | // Okay, the system allows quoted strings. We can quote most anything, the |
| 151 | // only characters that need escaping are " and \n. |
| 152 | assert(Name.find_first_of("\n\"") != StringRef::npos); |
| 153 | return appendMangledQuotedName(OutName, Name); |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 156 | /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require |
| 157 | /// a suffix on their name indicating the number of words of arguments they |
| 158 | /// take. |
| 159 | static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName, |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 160 | const Function *F, const DataLayout &TD) { |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 161 | // Calculate arguments size total. |
| 162 | unsigned ArgWords = 0; |
| 163 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 164 | AI != AE; ++AI) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 165 | Type *Ty = AI->getType(); |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 166 | // 'Dereference' type in case of byval parameter attribute |
| 167 | if (AI->hasByValAttr()) |
| 168 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 169 | // Size should be aligned to DWORD boundary |
| 170 | ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4; |
| 171 | } |
| 172 | |
| 173 | raw_svector_ostream(OutName) << '@' << ArgWords; |
| 174 | } |
| 175 | |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 176 | |
| 177 | /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix |
| 178 | /// and the specified global variable's name. If the global variable doesn't |
| 179 | /// have a name, this fills in a unique name for the global. |
| 180 | void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 181 | const GlobalValue *GV, |
| 182 | bool isImplicitlyPrivate) { |
Chris Lattner | ff24005 | 2010-01-17 18:52:16 +0000 | [diff] [blame] | 183 | ManglerPrefixTy PrefixTy = Mangler::Default; |
| 184 | if (GV->hasPrivateLinkage() || isImplicitlyPrivate) |
| 185 | PrefixTy = Mangler::Private; |
Bill Wendling | 32811be | 2012-08-17 18:33:14 +0000 | [diff] [blame] | 186 | else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage()) |
Chris Lattner | ff24005 | 2010-01-17 18:52:16 +0000 | [diff] [blame] | 187 | PrefixTy = Mangler::LinkerPrivate; |
| 188 | |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 189 | // If this global has a name, handle it simply. |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 190 | if (GV->hasName()) { |
| 191 | getNameWithPrefix(OutName, GV->getName(), PrefixTy); |
| 192 | } else { |
| 193 | // Get the ID for the global, assigning a new one if we haven't got one |
| 194 | // already. |
| 195 | unsigned &ID = AnonGlobalIDs[GV]; |
| 196 | if (ID == 0) ID = NextAnonGlobalID++; |
Chris Lattner | 0e7ab8c | 2010-01-13 07:01:09 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 198 | // Must mangle the global into a unique ID. |
| 199 | getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy); |
| 200 | } |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 202 | // If we are supposed to add a microsoft-style suffix for stdcall/fastcall, |
| 203 | // add it. |
| 204 | if (Context.getAsmInfo().hasMicrosoftFastStdCallMangling()) { |
| 205 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 206 | CallingConv::ID CC = F->getCallingConv(); |
| 207 | |
| 208 | // fastcall functions need to start with @. |
| 209 | // FIXME: This logic seems unlikely to be right. |
| 210 | if (CC == CallingConv::X86_FastCall) { |
| 211 | if (OutName[0] == '_') |
| 212 | OutName[0] = '@'; |
| 213 | else |
| 214 | OutName.insert(OutName.begin(), '@'); |
| 215 | } |
| 216 | |
| 217 | // fastcall and stdcall functions usually need @42 at the end to specify |
| 218 | // the argument info. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 219 | FunctionType *FT = F->getFunctionType(); |
Chris Lattner | 8a29fa6 | 2010-03-12 21:03:47 +0000 | [diff] [blame] | 220 | if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) && |
| 221 | // "Pure" variadic functions do not receive @0 suffix. |
| 222 | (!FT->isVarArg() || FT->getNumParams() == 0 || |
| 223 | (FT->getNumParams() == 1 && F->hasStructRetAttr()))) |
| 224 | AddFastCallStdCallSuffix(OutName, F, TD); |
| 225 | } |
| 226 | } |
Chris Lattner | 5b7dfee | 2009-09-11 05:40:42 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Chris Lattner | 73ff564 | 2010-03-12 18:55:20 +0000 | [diff] [blame] | 229 | /// getSymbol - Return the MCSymbol for the specified global value. This |
| 230 | /// symbol is the main label that is the address of the global. |
| 231 | MCSymbol *Mangler::getSymbol(const GlobalValue *GV) { |
| 232 | SmallString<60> NameStr; |
| 233 | getNameWithPrefix(NameStr, GV, false); |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 234 | return Context.GetOrCreateSymbol(NameStr.str()); |
Chris Lattner | 73ff564 | 2010-03-12 18:55:20 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |