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