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